get descriptor from instance

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Feb 18 18:15:10 EST 2009


En Wed, 18 Feb 2009 20:48:17 -0200, Alan G Isaac <aisaac at american.edu>  
escribió:

> What is a reliable way to get the
> the property object for an instance attribute?
> (Or more generally, to get the descriptor
> controlling an attribute?)
>
> If class ``A`` defines the property ``x`` and ``a``
> in an instance of ``A``, then you can just use
> ``type(a).__dict__['x']`` to get the property object.
> But this will not work if the property is inherited.

type(a).x

py> class A(object):
...   def getx(self): return 1
...   x = property(getx)
...
py> class B(A):
...   pass
...
py> b=B()
py> b.x
1
py> B.x
<property object at 0x00B8A690>
py> type(b).x
<property object at 0x00B8A690>
py> getattr(type(b),'x')
<property object at 0x00B8A690>
py>

-- 
Gabriel Genellina




More information about the Python-list mailing list