Docstrings and class Attributes

Terry Reedy tjreedy at
Mon Aug 8 20:17:38 CEST 2011

On 8/8/2011 8:37 AM, Nick wrote:
> Is it possible to put a doc string on a class attribute?

Class and function docstrings are generated from string expression 
statements at the beginning of a suite.

> class Test (object):
>      '''classx'''
>      fred = 10
>      '''attribute'''

This is two statements that have no particular connection with each other.

> print Test.__doc__
> print Test.fred.__doc__
> This code produces this output
> classx
> int(x[, base]) ->  integer
If an instance does not have a requested attribute, it is looked up on 
its class (and superclasses). Since all classes are subclasses of object 
and that has a docstring, I guess everything will.

Terry Jan Reedy

More information about the Python-list mailing list