How to get instance given a method of the instance?

user975135 picture user975135 · Jan 29, 2013 · Viewed 8.8k times · Source
class MyClass:
    def myMethod(self):
        pass

myInstance = MyClass()

methodReference = myInstance.myMethod

Now can you get a reference to myInstance if you now only have access to methodReference?

Answer

dusan picture dusan · Jan 29, 2013

Try this:

methodReference.im_self

If you are using Python 3:

methodReference.__self__