proxy class and __add__ method
Magnus Schuster
magnusschuster at yahoo.com
Wed Jul 30 06:48:45 EDT 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
Reference.
>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?
Regards,
Magnus
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):
__metaclass__=meta;
def __init__( self, subject ):
self.__subject = subject
def __getattr__( self, name ):
print "proxy.__getattr__"
return getattr( self.__subject, name )
prx_i=proxy(1)
k=prx_i+1
--- 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: http://www.nabble.com/proxy-class-and-__add__-method-tp18715799p18730676.html
Sent from the Python - python-list mailing list archive at Nabble.com.
More information about the Python-list
mailing list