Getting the docstring of a property

Marc Boeren m.boeren at guidance.nl
Wed Aug 11 08:31:54 EDT 2004


> How do you get the docstring of a property? Suppose I have 
> the following:
> 
> class Foo:
>     def _prop_get(self):
>         return 1
>     prop = property(_prop_get, doc="This is the docstring")

If I try that in the interactive interpreter, and then do help (I love
help :)

>>> help(Foo)
Help on class Foo in module __main__:

class Foo
 |  Properties defined here:
 |
 |  prop
 |      This is the docstring
 |
 |      <get> = _prop_get(self)

This means I can get at the docstring somehow, as help obviously can
(and as I would have expected :-)

The thing is: foo.prop is an integer, and you don't want the docstring
of an integer, but of a property. This means that you need to look at
the class-level docstring for prop, and not the instance-level
docstring. Translated: try Foo.prop.
>From the prompt:

>>> Foo.prop.__doc__
'This is the docstring'

or, if you only have the instance at hand and don't know the class yet:

>>> foo.__class__.prop.__doc__
'This is the docstring'

HTH, Mc!



More information about the Python-list mailing list