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)
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
JF Dion
  • 4,014
  • 2
  • 24
  • 34
  • 7
    Thera are also many more answers to this at the [earlier question](http://stackoverflow.com/questions/3898572/) of which this is a duplicate. – Alex Dupuy Jul 25 '14 at 18:42
  • Possible duplicate of [What is the standard Python docstring format?](https://stackoverflow.com/questions/3898572/what-is-the-standard-python-docstring-format) – zvone Dec 13 '17 at 18:59

4 Answers4

232

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
"""
starball
  • 20,030
  • 7
  • 43
  • 238
Steven
  • 28,002
  • 5
  • 61
  • 51
  • 9
    what do you do if you need to break a line for a long description? How would that look? – Skylar Saveland Sep 03 '12 at 02:14
  • 6
    See reStructuredText reference, and field lists in particular: http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#field-lists – Steven Sep 03 '12 at 08:54
  • 1
    From version 1.3, Sphinx also supports a bit nicer format via the `sphinx.ext.napoleon` extension. – Petri Jul 11 '14 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. – krumpelstiltskin Feb 24 '16 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 '16 at 16:34
  • What about links to other classes, methods (in this class and others) and so on? I could not find reStructuredText information detailing this. – user118967 Jan 17 '20 at 20:37
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.

Noémien Kocher
  • 1,324
  • 15
  • 17
confused00
  • 2,556
  • 21
  • 39
25

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 '')
    """
srgerg
  • 18,719
  • 4
  • 57
  • 39
  • 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 '13 at 08: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 01 '14 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.

David Cain
  • 16,484
  • 14
  • 65
  • 75