๐ŸŒ
Readthedocs
numpydoc.readthedocs.io โ€บ en โ€บ latest โ€บ format.html
Style guide โ€” numpydoc v1.11.1.dev1+gca74aae44 Manual
If a method has an equivalent function (which is the case for many ndarray methods for example), the function docstring should contain the detailed documentation, and the method docstring should refer to it. Only put brief summary and See Also sections in the method docstring. The method should use a Returns or Yields section, as appropriate. Instances of classes that are part of the NumPy API (for example np.r_ np.c_, np.index_exp, etc.) may require some care.
๐ŸŒ
Sphinx
sphinx-doc.org โ€บ en โ€บ master โ€บ usage โ€บ extensions โ€บ example_numpy.html
Example NumPy Style Python Docstrings โ€” Sphinx documentation
Attributes ---------- module_level_variable1 : int Module level variables may be documented in either the ``Attributes`` section of the module docstring, or in an inline docstring immediately following the variable. Either form is acceptable, but the two should not be mixed. Choose one convention to document module level variables and be consistent with it. .. _NumPy docstring standard: https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard """ module_level_variable1 = 12345 module_level_variable2 = 98765 """int: Module level variable documented inline.
๐ŸŒ
Medium
mr-amit.medium.com โ€บ numpy-docstring-explained-3d2e3b3f017a
NumPy Docstring Explained. If you think you need to spend $2,000โ€ฆ | by It's Amit | Medium
March 6, 2025 - A docstring is a multi-line string in Python that acts as a guide to explain what a module, function, class, or method does. Think of it as a small note left behind to make your (and everyone elseโ€™s) life easier when reading or using the code.
๐ŸŒ
Readthedocs
sphinxcontrib-napoleon.readthedocs.io โ€บ en โ€บ latest โ€บ example_numpy.html
Example NumPy Style Python Docstrings โ€” napoleon 0.7 documentation
Attributes ---------- module_level_variable1 : int Module level variables may be documented in either the ``Attributes`` section of the module docstring, or in an inline docstring immediately following the variable. Either form is acceptable, but the two should not be mixed. Choose one convention to document module level variables and be consistent with it. .. _NumPy Documentation HOWTO: https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt """ module_level_variable1 = 12345 module_level_variable2 = 98765 """int: Module level variable documented inline.
๐ŸŒ
Lsst
developer.lsst.io โ€บ python โ€บ numpydoc.html
Documenting Python APIs with docstrings โ€” LSST DM Developer Guide main documentation
We organize Python docstrings into sections that appear in a common order. This format is based on the original Numpydoc Style Guide (used by NumPy, SciPy, and Astropy, among other scientific Python packages), though this style guide includes several DM-specific clarifications.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.19 โ€บ docs โ€บ howto_document.html
A Guide to NumPy/SciPy Documentation โ€” NumPy v1.19 Manual
For example, the Yields section ... it in the documentation to avoid confusion. A documentation string (docstring) is a string that describes a module, function, class, or method definition....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.20 โ€บ docs โ€บ howto_document.html
A Guide to NumPy Documentation โ€” NumPy v1.20 Manual
January 31, 2021 - When using Sphinx in combination with the numpy conventions, you should use the numpydoc extension so that your docstrings will be handled correctly. For example, Sphinx will extract the Parameters section from your docstring and convert it into a field list.
๐ŸŒ
Readthedocs
numpydoc.readthedocs.io โ€บ en โ€บ v1.0.0 โ€บ format.html
numpydoc docstring guide โ€” numpydoc v1.0 Manual
If a method has an equivalent function (which is the case for many ndarray methods for example), the function docstring should contain the detailed documentation, and the method docstring should refer to it. Only put brief summary and See Also sections in the method docstring. The method should use a Returns or Yields section, as appropriate. Instances of classes that are part of the NumPy API (for example np.r_ np.c_, np.index_exp, etc.) may require some care.
๐ŸŒ
Python-sprints
python-sprints.github.io โ€บ pandas โ€บ guide โ€บ pandas_docstring.html
pandas docstring guide โ€” Python documentation
In rare occasions reST styles like bold text or itallics will be used in docstrings, but is it common to have inline code, which is presented between backticks. It is considered inline code: ... Python code, a module, function, built-in, type, literalโ€ฆ (e.g. os, list, numpy.abs, datetime.date, True)
Find elsewhere
Starred by 51 users
Forked by 8 users
Languages: Emacs Lisp
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Google Style: Ideal for projects where simplicity and readability are prioritized, especially within organizations using Google's style guides. NumPy Style: Best suited for projects requiring detailed and extensive documentation, common in data science and scientific computing.
๐ŸŒ
Pyansys
dev.docs.pyansys.com โ€บ doc-style โ€บ docstrings.html
Numpydoc docstrings โ€” PyAnsys developer's guide
Because you use the same markup in docstrings as you do in RST files, see Quick reStructuredText for a markup summary. Surround any text that you want to set apart as literal text (code entities) in double backticks to render it in a monospaced font within a gray box. Use double backticks to surround the names of files, folders, classes, methods, and variables. ... While the numpydoc style guide says to surround the names of classes, methods, and variables in a single backtick, you must use double backticks.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.21 โ€บ docs โ€บ howto_document.html
A Guide to NumPy Documentation โ€” NumPy v1.21 Manual
June 22, 2021 - When using Sphinx in combination with the numpy conventions, you should use the numpydoc extension so that your docstrings will be handled correctly. For example, Sphinx will extract the Parameters section from your docstring and convert it into a field list.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ how-to-write-numpy-style-docstrings-a092121403ba
How to Write NumPy-Style Docstrings | Python in Plain English
May 25, 2022 - As so often in the Python world, there are a few best practices on how to do something, in this case how to write docstrings properly. Three main styles have evolved: the traditional style, the Google style, and the NumPy style. I advocate the NumPy style because it is very human-readable (in contrast to the traditional style) and it is widely used in the scientific and technical community, which is my own domain.
Top answer
1 of 6
1389

Formats

Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post.

Note that the reST is recommended by the PEP 287

There follows the main used formats for docstrings.

- Epytext

Historically a javadoc like style was prevalent, so it was taken as a base for Epydoc (with the called Epytext format) to generate documentation.

Example:

"""
This is a javadoc style.

@param param1: this is a first param
@param param2: this is a second param
@return: this is a description of what is returned
@raise keyError: raises an exception
"""

- reST

Nowadays, the probably more prevalent format is the reStructuredText (reST) format that is used by Sphinx to generate documentation. Note: it is used by default in JetBrains PyCharm (type triple quotes after defining a method and hit enter). It is also used by default as output format in Pyment.

Example:

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

- Google

Google has their own format that is often used. It also can be interpreted by Sphinx (ie. using Napoleon plugin).

Example:

"""
This is an example of Google style.

Args:
    param1: This is the first param.
    param2: This is a second param.

Returns:
    This is a description of what is returned.

Raises:
    KeyError: Raises an exception.
"""

Even more examples

- Numpydoc

Note that Numpy recommend to follow their own numpydoc based on Google format and usable by Sphinx.

"""
My numpydoc description of a kind
of very exhautive numpydoc format docstring.

Parameters
----------
first : array_like
    the 1st param name `first`
second :
    the 2nd param
third : {'value', 'other'}, optional
    the 3rd param, by default 'value'

Returns
-------
string
    a value in a string

Raises
------
KeyError
    when a key error
OtherError
    when an other error
"""

Converting/Generating

It is possible to use a tool like Pyment to automatically generate docstrings to a Python project not yet documented, or to convert existing docstrings (can be mixing several formats) from a format to an other one.

Note: The examples are taken from the Pyment documentation

2 of 6
354

The Google style guide contains an excellent Python style guide. It includes conventions for readable docstring syntax that offers better guidance than PEP-257. For example:

def square_root(n):
    """Calculate the square root of a number.

    Args:
        n: the number to get the square root of.
    Returns:
        the square root of n.
    Raises:
        TypeError: if n is not a number.
        ValueError: if n is negative.

    """
    pass

I like to extend this to also include type information in the arguments, as described in this Sphinx documentation tutorial. For example:

def add_value(self, value):
    """Add a new value.

       Args:
           value (str): the value to add.
    """
    pass
Author: emacsmirror
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ dev โ€บ howto-docs.html
How to contribute to the NumPy documentation โ€” NumPy v2.5.dev0 Manual
The NumPy Documentation has the details covered. API reference documentation is generated directly from docstrings in the code when the documentation is built.
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ 3-different-docstring-formats-for-python-d27be81e0d68
3 Different Docstring Formats for Python | by Yash Salvi | Better Programming
April 27, 2022 - The output for both docstrings looks similar, the main difference between the two styles is that Google uses indentation to separate sections, whereas NumPy uses underlines. NumPy style tends to require more vertical space, whereas Google-style ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ dev โ€บ howto-docs.html
How to contribute to the NumPy documentation โ€” NumPy v2.5 Manual
The NumPy Documentation has the details covered. API reference documentation is generated directly from docstrings in the code when the documentation is built.