>>> class A(object):
... def some(self):
... pass
...
>>> a=A()
>>> a.some
<bound method A.some of <__main__.A object at 0x7f0d6fb9c090>>
IOWでは、 "a.some"だけの手渡し後に "a"にアクセスする必要があります。Pythonでバインドされたメソッドのインスタンスを見つけるには?
>>> class A(object):
... def some(self):
... pass
...
>>> a=A()
>>> a.some
<bound method A.some of <__main__.A object at 0x7f0d6fb9c090>>
IOWでは、 "a.some"だけの手渡し後に "a"にアクセスする必要があります。Pythonでバインドされたメソッドのインスタンスを見つけるには?
あなたは特別な属性__self__
を使用することができます。詳細について
、ちょうどそれが完全に明らかにすることをinspect
module in the Python Standard Library.
>>> 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>
それはあなたを助けている場合、次のコードを試してみて、参照してください。
a.some.im_self
をあなたは私が推測する、このような何かしたい:
>>> a = A()
>>> m = a.some
>>> another_obj = m.im_self
>>> another_obj
<__main__.A object at 0x0000000002818320>
im_self
はクラスインスタンスオブジェクトです。
>>> a.some.__self__ is a
True
im_self
py3kに段階的に廃止されます:python 2.6を起動する
を参照してください:あなたは、Python 2を使用している場合は、Python 3があれば、同等のは他の人が投稿した 'im_self'でこれを使用します。 –
@Thomas:安定したpython2.xの私のために完璧に動作します – SilentGhost
私の間違い、私はあなたの答えを慎重に読んでいませんでした。 –