473,584 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of lambda functions in OOP, any alternative?

Hello all, sorry if this is a faq...

Problem: The intended effect is to override the method 'getattr' in a
way that i dont need to override the property explicitly too.

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=g etattr)

class Derived(Base):
def getattr(self):
return 2*self._attr

if __name__ == "__main__":
b = Base(4)
d = Derived(4)
print b.attr, d.attr

output>> 4 8 ... so this does not work as i would like it to.

First solution: This is not what i want.

class Derived(Base):
def getattr(self):
return 2*self._attr
attr = property(fget=g etattr)

Second solution: This is what i want, but...

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=l ambda self: self.getattr())

class Derived(Base):
def getattr(self):
return 2*self._attr

Question: Isn't there an *alternative* way to do it without the lambda
function?

Thanks in advance!

May 23 '06 #1
6 1738
Pablo ha escrito:
Hello all, sorry if this is a faq...

Problem: The intended effect is to override the method 'getattr' in a
way that i dont need to override the property explicitly too.

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=g etattr)

class Derived(Base):
def getattr(self):
return 2*self._attr

if __name__ == "__main__":
b = Base(4)
d = Derived(4)
print b.attr, d.attr

output>> 4 8 ... so this does not work as i would like it to. sorry, i meant the following sentence:
output>> 4 4 ... so this does not work as i would like it to.
First solution: This is not what i want.

class Derived(Base):
def getattr(self):
return 2*self._attr
attr = property(fget=g etattr)

Second solution: This is what i want, but...

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=l ambda self: self.getattr())

class Derived(Base):
def getattr(self):
return 2*self._attr

Question: Isn't there an *alternative* way to do it without the lambda
function?

Thanks in advance!


May 23 '06 #2
Le Mardi 23 Mai 2006 15:55, Pablo a écrit*:
Question: Isn't there an *alternative* way to do it without the lambda
function?

No, it's good, why shouldn't you use a lambda function ?

Note you can do something like this :

class _virtualgetter :
def __init__(self, name) : self._n =name
def _call__(self, vself) : return getattr(vself, 'get' + self._n)()

def vprop(name) : return property(fget=_ virtualgetter(n ame))

#anywhere
from ... import vprop

class Base(object):
def __init__(self, attr):
self._attr = attr
def getAttribute(se lf):
return self._attr
attr = vprop('Attribut e')

class Derived(Base):
def getAttribute(se lf):
return 2*self._attr

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
May 23 '06 #3
The reason i would like a different approach to the lambda function is
just a question of personal taste... i dont really like it.

thanx!

May 23 '06 #4
Pablo wrote:
Second solution: This is what i want, but...

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=l ambda self: self.getattr())

class Derived(Base):
def getattr(self):
return 2*self._attr

Question: Isn't there an *alternative* way to do it without the lambda
function?

Thanks in advance!


Simplest:

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
@property # single-arg property is a read-only thing.
def attr(self):
return self.getattr()

### Little longer; maybe more explicit (tastes vary):

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
def attr(self):
return self.getattr()
attr = property(fget=a ttr)
--Scott David Daniels
sc***********@a cm.org
May 24 '06 #5
Oh! Thanx! Great! this is what i was looking for! :)

Scott David Daniels ha escrito:
Pablo wrote:
Second solution: This is what i want, but...

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=l ambda self: self.getattr())

class Derived(Base):
def getattr(self):
return 2*self._attr

Question: Isn't there an *alternative* way to do it without the lambda
function?

Thanks in advance!


Simplest:

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
@property # single-arg property is a read-only thing.
def attr(self):
return self.getattr()

### Little longer; maybe more explicit (tastes vary):

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
def attr(self):
return self.getattr()
attr = property(fget=a ttr)
--Scott David Daniels
sc***********@a cm.org


May 26 '06 #6
Le Mercredi 24 Mai 2006 22:37, Scott David Daniels a écrit*:
* * *class Base(object):
* * * * *def __init__(self, attr):
* * * * * * *self._attr = attr
* * * * *def getattr(self):
* * * * * * *return self._attr
* * * * *def attr(self):
* * * * * * *return self.getattr()
* * * * *attr = property(fget=a ttr)


but this has one drawback IMO, if you also want a virtual setter, you won'tbe
able to override both of them. This convention would be better to avoid
lambdas :

class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
def __gattr(self): # double _ shows that this method is not virtual
return self.getattr()
def setattr(self,v) :
self._attr = v
def __sattr(self,v) :
return self.setattr(v)
attr = property(fget=_ _gattr, fset=__sattr)

But, this is too verbose for me, I would opt for the lambda syntax :).

_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
May 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
3644
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions are constructed on the fly. Now my problem is lazy evaluation. Or at least I think it is. :-)
26
3476
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some of the ones I looked, classified by how I would rewrite them (if I could): * Rewritable as def statements (<name> = lambda <args>: <expr>...
181
8740
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
267
10613
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
0
7829
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8331
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8200
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5379
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1441
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.