How to use the docstring in this property example

Peter Otten __peter__ at web.de
Thu Jan 21 12:15:09 EST 2016


Robert wrote:

> Hi,
> 
> I read below code snippet on link:
> https://docs.python.org/2/library/functions.html#property
> 
> --------------
> class C(object):
>     def __init__(self):
>         self._x = None
> 
>     def getx(self):
>         return self._x
> 
>     def setx(self, value):
>         self._x = value
> 
>     def delx(self):
>         del self._x
> 
>     x = property(getx, setx, delx, "I'm the 'x' property.")
> If c is an instance of C, c.x will invoke the getter, c.x = value will
> invoke the setter and del c.x the deleter.
> 
> If given, doc will be the docstring of the property attribute.
> ////////////////
> 
> I can use these:
> c.x
> c.x=42
> del c.x
> 
> but I don't know how to get the doctring from the property attribute.
> Could you show me how to do that?

>>> c = C()
>>> type(c).x.__doc__
"I'm the 'x' property."

But usually you'll see it as part of the help on c:

>>> help(c)
Help on C in module __main__ object:

class C(__builtin__.object)
[...]
 |  Data descriptors defined here:
[...]
 |  x
 |      I'm the 'x' property.






More information about the Python-list mailing list