Use of lambda functions in OOP, any alternative?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pablo

    #1

    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!

  • Pablo

    #2
    Re: Use of lambda functions in OOP, any alternative?

    Pablo ha escrito:
    [color=blue]
    > 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.[/color]
    sorry, i meant the following sentence:
    output>> 4 4 ... so this does not work as i would like it to.[color=blue]
    >
    > 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![/color]

    Comment

    • Maric Michaud

      #3
      Re: Use of lambda functions in OOP, any alternative?

      Le Mardi 23 Mai 2006 15:55, Pablo a écrit :[color=blue]
      > Question: Isn't there an *alternative* way to do it without the lambda
      > function?[/color]
      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

      Comment

      • Pablo

        #4
        Re: Use of lambda functions in OOP, any alternative?

        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!

        Comment

        • Scott David Daniels

          #5
          Re: Use of lambda functions in OOP, any alternative?

          Pablo wrote:
          [color=blue]
          > 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![/color]

          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
          scott.daniels@a cm.org

          Comment

          • Pablo

            #6
            Re: Use of lambda functions in OOP, any alternative?

            Oh! Thanx! Great! this is what i was looking for! :)

            Scott David Daniels ha escrito:
            [color=blue]
            > Pablo wrote:
            >[color=green]
            > > 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![/color]
            >
            > 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
            > scott.daniels@a cm.org[/color]

            Comment

            • Maric Michaud

              #7
              Re: Use of lambda functions in OOP, any alternative?

              Le Mercredi 24 Mai 2006 22:37, Scott David Daniels a écrit :[color=blue]
              >      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)[/color]

              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

              Comment

              Working...