我尝试在python 参考的示例代码下面运行。
>>> class Meta(type):
... def __getattribute__(*args):
... print "Metaclass getattribute invoked"
... return type.__getattribute__(*args)
...
>>> class C(object):
... __metaclass__ = Meta
... def __len__(self):
... return 10
... def __getattribute__(*args):
... print "Class getattribute invoked"
... return object.__getattribute__(*args)
然后我测试了下面的代码:
In [16]: c = C()
class getattribute invoked
class getattribute invoked
In [17]: c
Class getattribute invoked
Class getattribute invoked
Out[17]: Class getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
Class getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
Metaclass getattribute invoked
<__main__.C at 0x29448d0>
任何人都可以对输出有明确的解释吗?我看到显示了很多“Metaclass getattribute invoked”,这意味着__getattribute__()
被调用了很多次。