Use of lambda functions in OOP, any alternative?

Pablo incall at msn.com
Thu May 25 20:24:21 EDT 2006


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=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
> scott.daniels at acm.org




More information about the Python-list mailing list