mrk*_*afk 35 python methods class
>>> class A(object):
... def some(self):
... pass
...
>>> a=A()
>>> a.some
<bound method A.some of <__main__.A object at 0x7f0d6fb9c090>>
Run Code Online (Sandbox Code Playgroud)
IOW,我需要在仅仅移交"a.some"后才能访问"a".
Sil*_*ost 41
启动python 2.6你可以使用特殊属性__self__
:
>>> a.some.__self__ is a
True
Run Code Online (Sandbox Code Playgroud)
im_self
在py3k中逐步淘汰.
有关详细信息,请参阅Python标准库中的inspect
模块.
>>> class A(object):
... def some(self):
... pass
...
>>> a = A()
>>> a
<__main__.A object at 0x7fa9b965f410>
>>> a.some
<bound method A.some of <__main__.A object at 0x7fa9b965f410>>
>>> dir(a.some)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
>>> a.some.im_self
<__main__.A object at 0x7fa9b965f410>
Run Code Online (Sandbox Code Playgroud)