Re: proxy class and __add__ method

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

    Re: proxy class and __add__ method

    En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster
    <magnusschuster @yahoo.comescri bi�:
    Hello,
    I have written the following small proxy class which I expect to pass all
    function calls to the 'original' object:
    >
    --- BEGIN ---
    class proxy(object):
    def __init__( self, subject ):
    self.__subject = subject
    def __getattr__( self, name ):
    return getattr( self.__subject, name )
    >
    prx_i=proxy(1)
    print hasattr(prx_i,' __add__')
    j=prx_i.__add__ (1)
    k=prx_i+1
    --- END ---
    >
    Actually the "hasattr(prx_i, '__add__')" returns "True" as expected, and
    "j=prx_i.__add_ _(1)" sets j=2.
    >
    But "k=prx_i+1" raises a
    <type 'exceptions.Typ eError'>: unsupported operand type(s) for +: 'proxy'
    and 'int'.
    >
    How is this addition different from the previous line "j=..."? And how
    can I
    modify the proxy class so that all methods are passed on, which are not
    explicitly overloaded?
    __magic__ methods on new style classes are searched in the class, *not* in
    the instance. prx_i+1 looks for __add__ in type(prx_i), that is, in the
    proxy class. Try implementing a similar __getattr__ method in a metaclass.

    --
    Gabriel Genellina

  • Rhamphoryncus

    #2
    Re: proxy class and __add__ method

    On Jul 29, 10:23 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
    wrote:
    En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster  
    <magnusschus... @yahoo.comescri bi :
    >
    >
    >
    Hello,
    I have written the following small proxy class which I expect to pass all
    function calls to the 'original' object:
    >
    --- BEGIN ---
    class proxy(object):
        def __init__( self, subject ):
            self.__subject = subject
        def __getattr__( self, name ):
            return getattr( self.__subject, name )
    >
    prx_i=proxy(1)
    print hasattr(prx_i,' __add__')
    j=prx_i.__add__ (1)
    k=prx_i+1
    --- END ---
    >
    Actually the "hasattr(prx_i, '__add__')" returns "True" as expected, and
    "j=prx_i.__add_ _(1)" sets j=2.
    >
    But "k=prx_i+1" raises a
    <type 'exceptions.Typ eError'>: unsupported operand type(s) for +: 'proxy'
    and 'int'.
    >
    How is this addition different from the previous line "j=..."? And how  
    can I
    modify the proxy class so that all methods are passed on, which are not
    explicitly overloaded?
    >
    __magic__ methods on new style classes are searched in the class, *not* in  
    the instance. prx_i+1 looks for __add__ in type(prx_i), that is, in the  
    proxy class.
    This much is true.

    Try implementing a similar __getattr__ method in a metaclass.
    But I don't think they use __getattr__.. they bypass it. Effectively
    they catch the assignment to __add__ and cache it. You'll have to
    always define it in the class and have it be ineffectual in some cases.

    Comment

    • Gabriel Genellina

      #3
      Re: proxy class and __add__ method

      En Wed, 30 Jul 2008 14:54:51 -0300, Rhamphoryncus <rhamph@gmail.c om>
      escribió:
      On Jul 29, 10:23 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
      wrote:
      >En Tue, 29 Jul 2008 13:13:51 -0300, Magnus Schuster  
      ><magnusschus.. .@yahoo.comescr ibi :
      >>
      I have written the following small proxy class which I expect to pass
      >all
      function calls to the 'original' object:
      >>
      --- BEGIN ---
      class proxy(object):
          def __init__( self, subject ):
              self.__subject = subject
          def __getattr__( self, name ):
              return getattr( self.__subject, name )
      >>
      But "k=prx_i+1" raises a
      <type 'exceptions.Typ eError'>: unsupported operand type(s) for +:
      >'proxy'
      and 'int'.
      >>
      How is this addition different from the previous line "j=..."? And
      >>how  
      >>can I
      >>modify the proxy class so that all methods are passed on, which are not
      >>explicitly overloaded?
      >>
      >Try implementing a similar __getattr__ method in a metaclass.
      >
      But I don't think they use __getattr__.. they bypass it. Effectively
      they catch the assignment to __add__ and cache it. You'll have to
      always define it in the class and have it be ineffectual in some cases.
      Ouch, yes, thanks, I noticed the fact after some testing.

      --
      Gabriel Genellina

      Comment

      Working...