經過在2.7.9
Couldn 「找不到任何辦法繞過調用__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__
#...
在擱將這個g的那個文件,我把它改名爲一個合適的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
和hasattr
讓每個這些名稱 - >相同的結果。每次調用__getattribute__
。
所以,如果任何人有其他的想法...我懶得去看看這個C代碼裏面,但我相信答案在某處。
因此,無論有什麼我沒有得到正確的,或文件說謊。
哪些方法分配給插槽? –
我認爲列出的每個方法[here](http://docs.python.org/reference/datamodel.html#specialnames)。無論如何,考慮到這隻適用於使用「其語法」調用該方法的情況。例如'a + 5'不會調用'__getattribute__',而'a .__ add __(5)'*會*調用它。 無論何時使用點('.')來訪問屬性,基本上都會調用__getattribute__'。 – Bakuriu
@Bakuriu:非常豐富,謝謝。 – porgarmingduod