2011-01-13 15 views

Respuesta

30

A partir de Python 2.6 se puede utilizar el atributo especial __self__:

>>> a.some.__self__ is a 
True 

im_self es reducida progresivamente en py3k.

Para más detalles, ver el inspect module in the Python Standard Library.

+1

Sólo para que quede todo claro: Utilice esta opción si tiene Python 3. Si está utilizando Python 2, el equivalente es el 'im_self' que otros han publicado. –

+2

@Thomas: funciona perfectamente bien para mí en python2.x estable – SilentGhost

+0

Mi error, no había leído su respuesta con cuidado. –

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

Try siguiente código y ver si le ayuda a:

a.some.im_self 
3

quieres algo como esto supongo:

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

im_self es el objeto de instancia de la clase.

Cuestiones relacionadas