Getting the docstring of a property

John Roth newsgroups at jhrothjr.com
Wed Aug 11 08:31:53 EDT 2004


"Andrew Durdin" <adurdin at gmail.com> wrote in message
news:mailman.1493.1092226382.5135.python-list at python.org...
> 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")
>
> foo = Foo()
> print foo.prop
> print foo.prop.__doc__
>
> This will display "1", followed by the docstring for an integer (which
> is the docstring for the int() function). How can I get the property's
> docstring as defined in the property() function?

You tried to access it from the instance. However, accessing it from the
instance invoked the property, returning 1, before it attempted to
access the docstring.

You need to get it from the class. That is:

Foo.prop.__doc__

should do what you want.

John Roth





More information about the Python-list mailing list