The Python Oracle

Using javadoc for Python documentation

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5

--

Chapters
00:00 Question
01:23 Accepted answer (Score 232)
02:40 Answer 2 (Score 79)
03:30 Answer 3 (Score 25)
03:56 Answer 4 (Score 1)
04:24 Thank you

--

Full question
https://stackoverflow.com/questions/5334...

Accepted answer links:
[reStructuredText]: http://docutils.sourceforge.net/rst.html
[Sphinx]: http://sphinx-doc.org
[sphinx.ext.autodoc]: http://sphinx-doc.org/ext/autodoc.html
[field lists]: http://sphinx-doc.org/domains.html#info-...

Answer 2 links:
[Google Python Style Guide]: https://github.com/google/styleguide/blo...
[Napolean]: http://sphinxcontrib-napoleon.readthedoc.../
[PEP257]: https://www.python.org/dev/peps/pep-0257/
[here]: http://sphinxcontrib-napoleon.readthedoc...

Answer 3 links:
[Python Enhancement Proposal 257]: http://www.python.org/dev/peps/pep-0257/

Answer 4 links:
[Documenting Python]: http://docs.python.org/devguide/document...
[reStructuredText]: http://docutils.sourceforge.net/rst.html

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #documentation #javadoc #docstring

#avk47



ACCEPTED ANSWER

Score 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
"""



ANSWER 2

Score 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.




ANSWER 3

Score 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 '')
    """



ANSWER 4

Score 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.