2011-01-13 14 views

答えて

30

あなたは特別な属性__self__を使用することができます。詳細について

、ちょうどそれが完全に明らかにすることをinspect module in the Python Standard Library.

+1

を参照してください:あなたは、Python 2を使用している場合は、Python 3があれば、同等のは他の人が投稿した 'im_self'でこれを使用します。 –

+2

@Thomas:安定したpython2.xの私のために完璧に動作します – SilentGhost

+0

私の間違い、私はあなたの答えを慎重に読んでいませんでした。 –

7
>>> 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> 
3

それはあなたを助けている場合、次のコードを試してみて、参照してください。

a.some.im_self 
3

をあなたは私が推測する、このような何かしたい:

>>> 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を起動する

関連する問題