JavaScript
x
8
1
class MyClass:
2
def myMethod(self):
3
pass
4
5
myInstance = MyClass()
6
7
methodReference = myInstance.myMethod
8
Now can you get a reference to myInstance
if you now only have access to methodReference
?
Advertisement
Answer
If you are using Python 3:
JavaScript
1
2
1
methodReference.__self__
2
Otherwise:
JavaScript
1
2
1
methodReference.im_self
2
and by a similar token, for the class:
JavaScript
1
2
1
methodReference.im_class
2
For this kind of code discovery you should install iPython and use tab, for instance, in your case myReference.+TAB would give:
JavaScript
1
3
1
In [6]: methodReference. methodReference.im_class
2
methodReference.im_func methodReference.im_self
3
Hence, you don’t need to worry about remembering things so much – you know that the method is probably provided by the function object and from the suggestions that iPython gives it’s usually obvious what method/attribute you’re looking for.