views:

616

answers:

5

Hello crowd,

What, in Your opinion is a meaningful docstring? What do You expect to be described there?

For example, consider this Python class's __init__:

def __init__(self, name, value, displayName=None, matchingRule="strict"):
    """
    name - field name
    value - field value
    displayName - nice display name, if empty will be set to field name
    matchingRule - I have no idea what this does, set to strict by default
    """

Do you find this meaningful? Post Your good/bad examples for all to know (and a general answer so it can be accepted).

+4  A: 

What should go there:

Anything that you can't tell from the method's signature. In this case the only bit useful is: displayName - if empty will be set to field name.

eglasius
+1 for "anything that you can't tell from the method's signature"
David Zaslavsky
Specific information that should go there is in the epydoc and sphinx projects. see http://epydoc.sourceforge.net/epydoc.html
S.Lott
+1  A: 

I like to use the documentation to describe in as much detail as possible what the function does, especially the behavior at corner cases (a.k.a. edge cases). Ideally, a programmer using the function should never have to look at the source code - in practice, that means that whenever another programmer does have to look at source code to figure out some detail of how the function works, that detail probably should have been mentioned in the documentation. As Freddy said, anything that doesn't add any detail to the method's signature probably shouldn't be in a documentation string.

David Zaslavsky
+3  A: 

I agree with "Anything that you can't tell from the method's signature". It might also mean to explain what a method/function returns.

You might also want to use Sphinx (and reStructuredText syntax) for documentation purposes inside your docstrings. That way you can include this in your documentation easily. For an example check out e.g. repoze.bfg which uses this extensively (example file, documentation example).

Another thing one can put in docstrings is also doctests. This might make sense esp. for module or class docstrings as you can also show that way how to use it and have this testable at the same time.

MrTopf
+1: Use RST notation with epydoc or sphinx.
S.Lott
+1  A: 

The most striking things I can think of to include in a docstring are the things that aren't obvious. Usually this includes type information, or capability requirements - eg. "Requires a file-like object". In some cases this will be evident from the signature, not so in other cases.

Another useful thing you can put in to your docstrings is a doctest.

sykora
+4  A: 

From PEP8:

Conventions for writing good documentation strings (a.k.a. "docstrings")
    are immortalized in PEP 257 [3].

    - Write docstrings for all public modules, functions, classes, and
      methods.  Docstrings are not necessary for non-public methods, but you
      should have a comment that describes what the method does.  This comment
      should appear after the "def" line.

    - PEP 257 describes good docstring conventions.  Note that most
      importantly, the """ that ends a multiline docstring should be on a line
      by itself, and preferably preceded by a blank line, e.g.:

    - For one liner docstrings, it's okay to keep the closing """ on the same
      line.
Xolve
This seems to cover the syntax but not semantics. Maybe there is a preferred style people like? Do You try to fill all @foobr keywords in docstrings?
Konrads
Xolve really should have posted a link to PEP 257: http://www.python.org/dev/peps/pep-0257/
John Fouhy