proxy class and __add__ method

Magnus Schuster magnusschuster at
Wed Jul 30 12:48:45 CEST 2008

>__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. 

With this explanation the behaviour is absolutely clear. Can I find some
documentation anywhere containing more background information how magic
functions are resolved? I haven't been successful with the Python Language

>Try implementing a similar __getattr__ method in a metaclass.

I am totally clueless how to do this. Below you find my attempt. Neither
__getattr__ nor __add__ of the metaclass 'meta' are ever called. May I ask
you for some more guidance or corrections to the code below?


2nd attempt:
--- BEGIN ---
class meta(type):
    def __getattr__( self, name ):
        print "meta.__getattr__"
        return getattr( self.__subject, name )
    def __add__( self, y ):
        print "meta.__add__"
        return self+y

class proxy(object):
    def __init__( self, subject ):
        self.__subject = subject
    def __getattr__( self, name ):
        print "proxy.__getattr__"
        return getattr( self.__subject, name )

--- END ---

The error is the same as before. Please note that I want to avoid
reimplementing all methods of 'int', because the proxy class should be
universal. (The meta.__add__ method is for testing only).

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

More information about the Python-list mailing list