Obscure __getattr__ behavior
D. Herzberg
Dominikus.Herzberg at web.de
Thu Feb 6 04:48:47 EST 2003
Hi,
while using __getattr__ in one of my programs, I stumbled
across the somewhat abscure behavior of __getattr__. Take
the following code:
class Test:
__attributes__= []
def __getattr__(self,name):
print "__getattr__(",name,")"
if name in self.__attributes__:
print "Lookup successful"
return self.__attributes__[name]
raise AttributeError, "%s not found" % (name)
If you type in
>>> t = Test()
>>> t
__getattr__( __repr__ )
<__main__.Test instance at 0x00778FA8>
>>>
When you run the code you know for sure that __getattr__ was
called with `__repr__' as a parameter; and you know for sure
that the lookup was not successfull -- so, what code is next?
Sure, it is the raise statement but for some reason it doesn't
raise anything. What makes the treatment of __repr__ special
once we are in the __getattr__ function?
Any explanations?
Gruss
Dominikus
More information about the Python-list
mailing list