Docstrings and class Attributes

Eric Snow ericsnowcurrently at gmail.com
Mon Aug 8 12:41:09 EDT 2011


On Mon, Aug 8, 2011 at 6:37 AM, Nick <nickle at gmail.com> wrote:
> Is it possible to put a doc string on a class attribute? Something
> like this

You can put a docstring on a property (which is a function):

class Test(object):
    @property
    def fred(self):
        "attribute"
        return 10

Python syntax supports implicitly building docstrings only for
modules, class definitions, and function definitions.

-eric

>
> class Test (object):
>    '''classx'''
>
>    fred = 10
>    '''attribute'''
>
> print Test.__doc__
> print Test.fred.__doc__
>
> This code produces this output
>
> classx
> int(x[, base]) -> integer
>
> Convert a string or number to an integer, if possible.  A floating
> point
> argument will be truncated towards zero (this does not include a
> string
> representation of a floating point number!)  When converting a string,
> use
> the optional base.  It is an error to supply a base when converting a
> non-string.  If base is zero, the proper base is guessed based on the
> string content.  If the argument is outside the integer range a
> long object will be returned instead.
>
> ===========
>
> So the class doc string is return, but no doc string for the
> attribute.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list