22

Is there a way to describe the module's data in a similar way that a docstring describes a module or a funcion?

class MyClass(object):
    def my_function():
        """This docstring works!"""
        return True
    my_list = []
    """This docstring does not work!"""
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
Bartosz Radaczyński
  • 18,396
  • 14
  • 54
  • 61

3 Answers3

15

To my knowledge, it is not possible to assign docstrings to module data members.

PEP 224 suggests this feature, but the PEP was rejected.

I suggest you document the data members of a module in the module's docstring:

# module.py:
"""About the module.

module.data: contains the word "spam"

"""

data = "spam"
codeape
  • 97,830
  • 24
  • 159
  • 188
11

It is possible to make documentation of module's data, with use of epydoc syntax. Epydoc is one of the most frequently used documentation tools for Python.

The syntax for documenting is #: above the variable initialization line, like this:

# module.py:

#: Very important data.
#: Use with caution.
#: @type: C{str}
data = "important data"

Now when you generate your documentation, data will be described as module variable with given description and type str. You can omit the @type line.

Dzinx
  • 55,586
  • 10
  • 60
  • 78
  • 1
    Even though this is cool, I was looking for exactly docstings (since these are the most helpful in the console environment using help builtin). Anyway thanx – Bartosz Radaczyński Oct 13 '08 at 13:43
10

As codeape explains, it's not possible to document general data members.

However, it is possible to document property data members:

class Foo:
  def get_foo(self): ...

  def set_foo(self, val): ...

  def del_foo(self): ...

  foo = property(get_foo, set_foo, del_foo, '''Doc string here''')

This will give a docstring to the foo attribute, obviously.

Dan Lenski
  • 76,929
  • 13
  • 76
  • 124