Use of lambda functions in OOP, any alternative?

P

Pablo

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=getattr)

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=getattr)

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=lambda 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!
 
P

Pablo

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=getattr)

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.
 
M

Maric Michaud

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(name))

#anywhere
from ... import vprop

class Base(object):
def __init__(self, attr):
self._attr = attr
def getAttribute(self):
return self._attr
attr = vprop('Attribute')

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

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
P

Pablo

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!
 
S

Scott David Daniels

Pablo said:
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=lambda 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=attr)


--Scott David Daniels
(e-mail address removed)
 
M

Maric Michaud

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=attr)

but this has one drawback IMO, if you also want a virtual setter, you won't be
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top