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)
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.
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:
Or extended with type information:
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):
Example taken from the Napolean documentation linked above.
A comprehensive example on all types of docstrings here.
The standard for python documentation strings is described in Python Enhancement Proposal 257.
The appropriate comment for your method would be something like