Docstrings and class Attributes

Chris Kaynor ckaynor at zindagigames.com
Mon Aug 8 21:22:33 CEST 2011


They are actually quite easy to get to. help() on both the class or instance
produces the docstring, and __doc__ on the property as accessed from the
class produces the docstring.

>>> class Test(object):
...     @property
...     def fred(self):
...             """*This is a docstring.*"""
...             return 1
...
>>> help(Test)
Help on class Test in module __main__:

class Test(__builtin__.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
 |
 |  fred
 |      *This is a docstring.*
>>> Test.fred.__doc__
'*This is a docstring*.'
>>> t = Test()
>>> t.fred.__doc__
'int(x[, base]) -> integer\n\n...'
>>> help(t)
Help on Test in module __main__ object:

class Test(__builtin__.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
 |
 |  fred
 |      *This is a docstring.*





Chris


On Mon, Aug 8, 2011 at 12:05 PM, Steven D'Aprano <
steve+comp.lang.python at pearwood.info> wrote:

> Ethan Furman wrote:
>
> > So if property docstrings are so hard to get to, what's the point in
> > having them?
>
> Hard to get, not impossible. But I have no idea really -- they don't seem
> very useful to me.
>
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110808/ed7a5461/attachment.html>


More information about the Python-list mailing list