如何获取实例给出的实例的方法? [英] How to get instance given a method of the instance?

查看:218
本文介绍了如何获取实例给出的实例的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class MyClass:
    def myMethod(self):
        pass

myInstance = MyClass()

methodReference = myInstance.myMethod

现在可以引用 myInstance 如果你现在只能访问 methodReference

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

推荐答案

methodReference.im_self

methodReference.im_class

对于这种代码发现,你应该安装iPython并使用tab,例如,在你的case myReference。+ TAB会给:

For this kind of code discovery you should install iPython and use tab, for instance, in your case myReference.+TAB would give:

In [6]: methodReference. methodReference.im_class 
methodReference.im_func   methodReference.im_self

因此,您不需要担心记住事情这么多 - 你知道该方法可能由函数对象提供,并从建议,iPython给它通常很明显你正在寻找什么方法/属性。

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.

这篇关于如何获取实例给出的实例的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆