So, reStructuredText is the recommended way for Python code documentation, if you try hard enough, you can find in the sphinx documentation how to normalize your function signature documentation. All given examples are single-line, but what if a parameter description is multi-line like the following ?
def f(a, b): """ Does something with a and b :param a: something simple :param b: well, it's not something simple, so it may require more than eighty chars """
What is the syntax/convention for that ? Should I indent or not ? will it break reSTructuredText rendering ?
Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line.
You can define a docstring with the help of triple-quotation mark. Add one in the beginning and second at the end of the string. Just like multiline comments, docstring can also overlap to multiple lines. Note: The strings defined using triple-quotation mark are docstring in Python.
A docstring is a string constant associated with any python object or module. The object may be a class, a method or a function. The docstring is written simply like multiline comments using multiline strings but it must be the first statement in the object's definition.
Comments are great for leaving notes for people working on your program. Docstrings provide documentation about functions, classes, and modules. Use docstrings to teach other developers how to use your program.
Good research effort from the Original Poster. It is a surprise that the canonical sphinx documentation does not give a multi-line example on params, despite the fact that multi-line document is inevitable due to the 79-character guideline in PEP8.
In practice, considering that your parameter name itself is typically a word
or even longer snake_case_words
, prefixed by the already lenghty <4 or 8+ spaces> :param
, it would be wise to make the next line indent for just one level (i.e. 4 spaces), which matches the "hanging indents" style metioned in PEP 8.
class Foo(object): def f(a, bionic_beaver, cosmic_cuttlefish): """ Does something. :param a: something simple :param bionic_beaver: well, it's not something simple, so it may require more than eighty chars, and more, and more :param cosmic_cuttlefish: Or you can just put all your multi-line sentences to start with SAME indentation. """
PS: You can see it in action in, for example, here. Sphinx can pick up those docstrings and generates docs without any issue.
Seems that if you indent by at least one level relative to the :param: directive, it will not break reSTructuredText rendering. Personally, I prefer to align all additional lines to the first description line of that parameter. Note, that reST will also ignore new lines and render your text without your line breaks.
Unfortunately, I could not find any source that would mention this issue or give an example of a multi-line :param: description.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With