如何在 Python 中找到绑定方法的实例? [英] How to find instance of a bound method in Python?

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

问题描述

<预><代码>>>>A类(对象):...定义一些(自我):... 经过...>>>a=A()>>>a.一些<绑定方法A.some of <__main__.A object at 0x7f0d6fb9c090>>

IOW,我需要在只交出a.some"后才能访问a".

解决方案

从 python 2.6 开始你可以使用特殊属性 __self__:

<预><代码>>>>a.some.__self__ 是一个真的

im_self 在 py3k 中被淘汰.

有关详细信息,请参阅 Python 标准库中的 inspect 模块.

>>> class A(object):  
...         def some(self):  
...                 pass  
...  
>>> a=A()  
>>> a.some  
<bound method A.some of <__main__.A object at 0x7f0d6fb9c090>>

IOW, I need to get access to "a" after being handed over only "a.some".

解决方案

Starting python 2.6 you can use special attribute __self__:

>>> a.some.__self__ is a
True

im_self is phased out in py3k.

For details, see the inspect module in the Python Standard Library.

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

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