Use of lambda functions in OOP, any alternative?

Maric Michaud maric at aristote.info
Fri May 26 15:21:10 CEST 2006


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



More information about the Python-list mailing list