Use of lambda functions in OOP, any alternative?
Pablo
incall at msn.com
Tue May 23 09:55:58 EDT 2006
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!
More information about the Python-list
mailing list