How to document a method with parameter(s)?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping
--
Chapters
00:00 Question
00:52 Accepted answer (Score 116)
01:18 Answer 2 (Score 239)
02:42 Answer 3 (Score 38)
03:45 Answer 4 (Score 12)
04:17 Thank you
--
Full question
https://stackoverflow.com/questions/9195...
Question links:
[PEP 257]: http://www.python.org/dev/peps/pep-0257/
Accepted answer links:
[numpy docstring conventions]: https://numpydoc.readthedocs.io/en/lates...
[Sphinx]: https://www.sphinx-doc.org/en/master/
Answer 2 links:
[Sphinx markup]: https://www.sphinx-doc.org/en/master/usa...
[readthedocs.org]: https://readthedocs.org
[paraphrase an example]: http://www.sphinx-doc.org/en/stable/doma...
[cross-referencing]: http://www.sphinx-doc.org/en/1.4.8/domai...
[Doxygen]: http://www.doxygen.nl/
[commands]: http://www.doxygen.nl/manual/commands.ht...
[similar question]: https://stackoverflow.com/questions/5334...
[similar answer]: https://stackoverflow.com/a/5339352/1174...
Answer 3 links:
[PEP 257 Docstring Conventions]: http://www.python.org/dev/peps/pep-0257/
[PEP 287 reStructuredText Docstring Format]: http://www.python.org/dev/peps/pep-0287/
[Epydoc: Automatic API Documentation Generation for Python]: http://epydoc.sourceforge.net/
[sphinx.ext.autodoc – Include documentation from docstrings]: http://sphinx.pocoo.org/ext/autodoc.html
[PyCharm has some nice support for docstrings]: http://www.jetbrains.com/pycharm/webhelp...
[type hints]: https://docs.python.org/3/library/typing...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #documentation #documentationgeneration
#avk47
ACCEPTED ANSWER
Score 125
Based on my experience, the numpy docstring conventions (PEP257 superset) are the most widely-spread followed conventions that are also supported by tools, such as Sphinx.
One example:
Parameters
----------
x : type
Description of parameter `x`.
ANSWER 2
Score 40
Conventions:
Tools:
- Epydoc: Automatic API Documentation Generation for Python
- sphinx.ext.autodoc – Include documentation from docstrings
- PyCharm has some nice support for docstrings
Update: Since Python 3.5 you can use type hints which is a compact, machine-readable syntax:
from typing import Dict, Union
def foo(i: int, d: Dict[str, Union[str, int]]) -> int:
"""
Explanation: this function takes two arguments: `i` and `d`.
`i` is annotated simply as `int`. `d` is a dictionary with `str` keys
and values that can be either `str` or `int`.
The return type is `int`.
"""
The main advantage of this syntax is that it is defined by the language and that it's unambiguous, so tools like PyCharm can easily take advantage from it.
ANSWER 3
Score 12
python doc strings are free-form, you can document it in any way you like.
Examples:
def mymethod(self, foo, bars):
"""
Does neat stuff!
Parameters:
foo - a foo of type FooType to bar with.
bars - The list of bars
"""
Now, there are some conventions, but python doesn't enforce any of them. Some projects have their own conventions. Some tools to work with docstrings also follow specific conventions.
ANSWER 4
Score 8
If you plan to use Sphinx to document your code, it is capable of producing nicely formatted HTML docs for your parameters with their 'signatures' feature. http://sphinx-doc.org/domains.html#signatures