1
猴子用python中的新方法修補類後,是否可以檢查此方法的身份?檢查猴子修補方法的身份
將函數指定爲方法後,我找不到任何方法來檢查其身份。所有的比較返回False
:
In [1]: class A(object):
...: pass
...:
In [2]: a = A()
In [3]: def b(inst):
...: pass
...:
In [4]: A.c = b
In [5]: a.c is b
Out[5]: False
In [6]: a.__class__.c is b
Out[6]: False
In [7]: A.c is b
Out[7]: False
In [8]: type(b)
Out[8]: function
In [9]: type(a.c)
Out[9]: instancemethod
In [10]: type(a.__class__.c)
Out[10]: instancemethod
In [11]: type(A.c)
Out[11]: instancemethod
Python 2裏,我想? – vaultah
注意:問題在於方法是以描述符的形式實現的。每次訪問一個包含實際函數的新對象時,即使'x.method is x.method'也會失敗。 – Bakuriu
@vaultah python 2和python 3。 – ARF