proxy class and __add__ method

Magnus Schuster magnusschuster at
Tue Jul 29 18:13:51 CEST 2008

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 )

print hasattr(prx_i,'__add__')
--- 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.TypeError'>: 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?

View this message in context:
Sent from the Python - python-list mailing list archive at

More information about the Python-list mailing list