Possible Duplicate:
Intercept operator lookup on metaclass
How can I intercept calls to python’s “magic” methods in new style classes?
Consider the following code:
class ClassA(object):
def __getattribute__(self, item):
print 'custom__getattribute__ - ' + item
return ''
def __str__(self):
print 'custom__str__'
return ''
a=ClassA()
print 'a.__str__: ',
a.__str__
print 'str(a): ',
str(a)
The output was surprising to me:
a.__str__: custom__getattribute__ - __str__
str(a): custom__str__
- Isn't
str(a)
supposed to be mapped to the magic methoda.__str__()
? - If I remove the custom
ClassA.__str__()
, thenClassA.__getattribute__()
still doesn't catch the call. How come?