In python 2.x take the following class:
class Person:
def __init__(self, name):
self.name = name
def myrepr(self):
return str(self.name)
def __getattr__(self, attr):
print('Fetching attr: %s' % attr)
if attr=='__repr__':
return self.myrepr
Now if you create an instance and echo it in the shell (to call __repr__
), like
p = Person('Bob')
p
You get
Fetching attr: __repr__
Bob
Without the __getattr__
overload you'd have just got the default <__main__.A instance at 0x7fb8800c6e18>
kind of thing.
My question is why is the built-in __getattr__
even capable of handling calls to __repr__
(and other builtins like that) if they are not elsewhere defined. These are not attributes they are operators, more like methods..
(This no longer works in python 3.x so I guess they got rid of the handling by __getattr__ of the builtins.)