2011-01-13 6 views

Odpowiedz

30

Uruchamianie Pythona 2.6 można użyć specjalnego atrybutu __self__:

>>> a.some.__self__ is a 
True 

im_self jest wycofywane w py3k.

Aby uzyskać szczegółowe informacje, zobacz inspect module in the Python Standard Library.

+1

Wystarczy zrobić to całkowicie jasne: Użyj tego jeśli masz Python 3. Jeśli używasz Python 2, odpowiednik jest 'im_self' że inni napisali. –

+2

@Thomas: działa doskonale dla mnie w stabilnym python2.x – SilentGhost

+0

Mój błąd, nie przeczytałem odpowiedzi wystarczająco dokładnie. –

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

Spróbuj poniższy kod i zobaczyć, czy to pomaga:

a.some.im_self 
3

Chcesz coś takiego Chyba:

>>> a = A() 
>>> m = a.some 
>>> another_obj = m.im_self 
>>> another_obj 
<__main__.A object at 0x0000000002818320> 

im_self jest przedmiotem instancja klasy.