1

After monkey patching a class with a new method in python, is it possible to check the identity of this method?

After assigning a function as a method, I cannot find any way to check its identity. All comparisons return 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
ARF
  • 7,420
  • 8
  • 45
  • 72

1 Answers1

1

Yes, use __func__ property of patched method:

>> type(a.c.__func__)
<class 'function'>
>> a.c.__func__ is c
True
toriningen
  • 7,196
  • 3
  • 46
  • 68