1

Given a method handle in Python 3, how do I retrieve the object it is part of?

class Myclass:
    def foo(self):
        print(self, 'foo')

m = Myclass()

method_handle = m.foo
print(method_handle) # -> <bound method Myclass.foo of <__main__.Myclass object at 0x7fb80220dd10>>

The method_handle object has a reference to the m instance somewhere. But if I only have the method_handle, how do I retrieve the m object?

JanKanis
  • 6,346
  • 5
  • 38
  • 42

1 Answers1

2

Never mind, I just found it: it is in method_handle.__self__.

JanKanis
  • 6,346
  • 5
  • 38
  • 42