Method objects are created each time you access them. Functions act as descriptors, returning a method object when their .__get__
method is called:
>>> What.__dict__['meth']
<function What.meth at 0x10a6f9c80>
>>> What.__dict__['meth'].__get__(What(), What)
<bound method What.meth of <__main__.What object at 0x10a6f7b10>>
If you're on Python 3.8 or later, you can use ==
equality testing instead. On Python 3.8 and later, two methods are equal if their .__self__
and .__func__
attributes are identical objects (so if they wrap the same function, and are bound to the same instance, both tested with is
).
Before 3.8, method ==
behaviour is inconsistent based on how the method was implemented - Python methods and one of the two C method types compare __self__
for equality instead of identity, while the other C method type compares __self__
by identity. See Python issue 1617161.
If you need to test that the methods represent the same underlying function, test their __func__
attributes:
>>> What.meth == What.meth # functions (or unbound methods in Python 2)
True
>>> What().meth == What.meth # bound method and function
False
>>> What().meth == What().meth # bound methods with *different* instances
False
>>> What().meth.__func__ == What().meth.__func__ # functions
True
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…