签入 2.7.9
找不到任何绕过对 的调用的方法,使用在or__getattribute__
上找到的任何神奇方法:object
type
# Preparation step: did this from the console
# magics = set(dir(object) + dir(type))
# got 38 names, for each of the names, wrote a.<that_name> to a file
# Ended up with this:
a.__module__
a.__base__
#...
把它放在那个文件的开头,我把它重命名为一个合适的python模块(asdf.py)
global_counter = 0
class Counter(object):
def __getattribute__(self, name):
# this will count how many times the method was called
global global_counter
global_counter += 1
return super(Counter, self).__getattribute__(name)
a = Counter()
# after this comes the list of 38 attribute accessess
a.__module__
#...
a.__repr__
#...
print global_counter # you're not gonna like it... it printer 38
然后我还尝试通过getattr
and hasattr
-> 相同的结果来获取每个名称。__getattribute__
每次都被调用。
因此,如果有人有其他想法……我懒得去查看 C 代码,但我确信答案就在某个地方。
所以要么是我做的不对,要么是文档在撒谎。