<分区> 分区>
Possible Duplicate:
Intercept operator lookup on metaclass
How can I intercept calls to python’s “magic” methods in new style classes?
考虑以下代码:
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)
输出结果令我惊讶:
a.__str__: custom__getattribute__ - __str__
str(a): custom__str__
str(a)不应该映射到魔法方法吗a.__str__()?- 如果我删除自定义
ClassA.__str__(),那么ClassA.__getattribute__()仍然没有接听电话。怎么会?
