Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docstrings for data?

Tags:

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!""" 
like image 545
Bartosz Radaczyński Avatar asked Oct 13 '08 12:10

Bartosz Radaczyński


People also ask

When should docstrings be used?

As mentioned above, Python docstrings are strings used right after the definition of a function, method, class, or module (like in Example 1). They are used to document our code. We can access these docstrings using the __doc__ attribute.

What should docstrings contain?

The docstring for a module function should include the same items as a class method: A brief description of what the function is and what it's used for. Any arguments (both required and optional) that are passed including keyword arguments.

Are docstrings only for functions?

Python documentation string or commonly known as docstring, is a string literal, and it is used in the class, module, function, or method definition.

What is the point of docstrings?

Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. It's specified in source code that is used, like a comment, to document a specific segment of code.


2 Answers

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" 
like image 139
codeape Avatar answered Sep 26 '22 00:09

codeape


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.

like image 43
DzinX Avatar answered Sep 23 '22 00:09

DzinX