174

I am currently beginning with Python and I have a strong PHP background and in PHP I have took the habit of using javadoc as a documentation template.

I was wondering if javadoc has its place as docstring documentation in Python. What are the established conventions and/or official guildelines here?

E.g. is something like this too elaborate to fit in the Python mindset or should I try to be as concise as possible?

"""
replaces template place holder with values

@param string timestamp     formatted date to display
@param string priority      priority number
@param string priority_name priority name
@param string message       message to display

@return string formatted string
"""

And if I am a bit too exhaustive should I go with something like this instead (where most of the documentation doesn't get printed through the __doc__ method)?

# replaces template place holder with values
#    
# @param string timestamp     formatted date to display
# @param string priority      priority number
# @param string priority_name priority name
# @param string message       message to display
#    
# @return string formatted string

def format(self, timestamp = '', priority = '', priority_name = '', message = ''):
    """
    replaces template place holder with values
    """
    values = {'%timestamp%' : timestamp,
              '%priorityName%' : priority_name,
              '%priority%' : priority,
              '%message%' : message}

    return self.__pattern.format(**values)
2

4 Answers 4

233

Have a look at the reStructuredText (also known as "reST") format, which is a plaintext/docstring markup format, and probably the most popular in the Python world. And you should certainly look at Sphinx, a tool to generate documentation from reStructuredText (used for eg. the Python documentation itself). Sphinx includes the possibility to extract documentation from the docstrings in your code (see sphinx.ext.autodoc), and recognizes reST field lists following certain conventions. This has probably become (or is becoming) the most popular way to do it.

Your example could look as follows:

"""Replace template placeholder with values.

:param timestamp: formatted date to display
:param priority: priority number
:param priority_name: priority name
:param message: message to display
:returns: formatted string
"""

Or extended with type information:

"""Replace template placeholder with values.

:param timestamp: formatted date to display
:type timestamp: str or unicode
:param priority: priority number
:type priority: str or unicode
:param priority_name: priority name
:type priority_name: str or unicode
:param message: message to display
:type message: str or unicode
:returns: formatted string
:rtype: str or unicode
"""
6
  • 9
    what do you do if you need to break a line for a long description? How would that look? Sep 3, 2012 at 2:14
  • 6
    See reStructuredText reference, and field lists in particular: docutils.sourceforge.net/docs/ref/rst/…
    – Steven
    Sep 3, 2012 at 8:54
  • 1
    From version 1.3, Sphinx also supports a bit nicer format via the sphinx.ext.napoleon extension.
    – Petri
    Jul 11, 2014 at 17:16
  • 4
    Could someone please point me to the best documentation specifying these special docstrings like ":param ____:" and ":returns:"? Such a document seems rather hard to find at the moment. Feb 24, 2016 at 15:24
  • I like this.. flat is better than nested. I just include the type in the param instead of having the type line on its own.
    – radtek
    May 10, 2016 at 16:34
79

Follow Google Python Style Guide. Note that Sphinx can also parse this format using the Napolean extension, which will come packaged with Sphinx 1.3 (this is also compatible with PEP257):

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Args:
        arg1 (int): Description of arg1
        arg2 (str): Description of arg2

    Returns:
        bool: Description of return value

    """
    return True

Example taken from the Napolean documentation linked above.

A comprehensive example on all types of docstrings here.

4
26

The standard for python documentation strings is described in Python Enhancement Proposal 257.

The appropriate comment for your method would be something like

def format(...):
    """Return timestamp string with place holders replaced with values.

    Keyword arguments:
    timestamp     -- the format string (default '')
    priority      -- priority number (default '')
    priority_name -- priority name (default '')
    message       -- message to display (default '')
    """
2
  • 17
    PEP257 doesn't tell anything on the actual formatting of the argument part. It just states that it should be written, and gives an example. But this is only an example. Therefore, I would definitively advise using the Sphinx convention, as you don't break PEP257 and you use a formatting that could be parsed by sphinx.
    – vaab
    Aug 19, 2013 at 8:46
  • 7
    Except the rst documentation presented above is ugly and has lots of redundant information for humans. I'd rather use a convention that makes my source code pleasant to read without being parsed first
    – confused00
    Dec 1, 2014 at 16:05
1

Take a look at Documenting Python, a page "aimed at authors and potential authors of documentation for Python."

In short, reStructuredText is what's used for documenting Python itself. The developer's guide contains a reST primer, style guide, and general advice for writing good documentation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.