NumPy Docstrings

Source

  1"""Docstring for the example.py module.
  2
  3Modules names should have short, all-lowercase names.  The module name may
  4have underscores if this improves readability.
  5
  6Every module should have a docstring at the very top of the file.  The
  7module's docstring may extend over multiple lines.  If your docstring does
  8extend over multiple lines, the closing three quotation marks must be on
  9a line by itself, preferably preceded by a blank line.
 10
 11"""
 12from __future__ import absolute_import, division, print_function
 13
 14import os  # standard library imports first
 15
 16import matplotlib as mpl
 17import matplotlib.pyplot as plt
 18import numpy as np
 19
 20# Do NOT import using *, e.g. from numpy import *
 21#
 22# Import the module using
 23#
 24#   import numpy
 25#
 26# instead or import individual functions as needed, e.g
 27#
 28#  from numpy import array, zeros
 29#
 30# If you prefer the use of abbreviated module names, we suggest the
 31# convention used by NumPy itself::
 32
 33
 34# These abbreviated names are not to be used in docstrings; users must
 35# be able to paste and execute docstrings after importing only the
 36# numpy module itself, unabbreviated.
 37
 38
 39def foo(var1, var2, *args, long_var_name="hi", **kwargs):
 40    r"""Summarize the function in one line.
 41
 42    Several sentences providing an extended description. Refer to
 43    variables using back-ticks, e.g. `var`.
 44
 45    Parameters
 46    ----------
 47    var1 : array_like
 48        Array_like means all those objects -- lists, nested lists, etc. --
 49        that can be converted to an array.  We can also refer to
 50        variables like `var1`.
 51    var2 : int
 52        The type above can either refer to an actual Python type
 53        (e.g. ``int``), or describe the type of the variable in more
 54        detail, e.g. ``(N,) ndarray`` or ``array_like``.
 55    *args : iterable
 56        Other arguments.
 57    long_var_name : {'hi', 'ho'}, optional
 58        Choices in brackets, default first when optional.
 59    **kwargs : dict
 60        Keyword arguments.
 61
 62    Returns
 63    -------
 64    type
 65        Explanation of anonymous return value of type ``type``.
 66    describe : type
 67        Explanation of return value named `describe`.
 68    out : type
 69        Explanation of `out`.
 70    type_without_description
 71
 72    Other Parameters
 73    ----------------
 74    only_seldom_used_keywords : type
 75        Explanation.
 76    common_parameters_listed_above : type
 77        Explanation.
 78
 79    Raises
 80    ------
 81    BadException
 82        Because you shouldn't have done that.
 83
 84    See Also
 85    --------
 86    numpy.array : Relationship (optional).
 87    numpy.ndarray : Relationship (optional), which could be fairly long, in
 88                    which case the line wraps here.
 89    numpy.dot, numpy.linalg.norm, numpy.eye
 90
 91    Notes
 92    -----
 93    Notes about the implementation algorithm (if needed).
 94
 95    This can have multiple paragraphs.
 96
 97    You may include some math:
 98
 99    .. math:: X(e^{j\omega } ) = x(n)e^{ - j\omega n}
100
101    And even use a Greek symbol like :math:`\omega` inline.
102
103    References
104    ----------
105    Cite the relevant literature, e.g. [1]_.  You may also cite these
106    references in the notes section above.
107
108    .. [1] O. McNoleg, "The integration of GIS, remote sensing,
109       expert systems and adaptive co-kriging for environmental habitat
110       modelling of the Highland Haggis using object-oriented, fuzzy-logic
111       and neural-network techniques," Computers & Geosciences, vol. 22,
112       pp. 585-588, 1996.
113
114    Examples
115    --------
116    These are written in doctest format, and should illustrate how to
117    use the function.
118
119    >>> a = [1, 2, 3]
120    >>> print([x + 3 for x in a])
121    [4, 5, 6]
122    >>> print("a\nb")
123    a
124    b
125    """
126    # After closing class docstring, there should be one blank line to
127    # separate following codes (according to PEP257).
128    # But for function, method and module, there should be no blank lines
129    # after closing the docstring.
130    pass

Rendered

Docstring for the example.py module.

Modules names should have short, all-lowercase names. The module name may have underscores if this improves readability.

Every module should have a docstring at the very top of the file. The module’s docstring may extend over multiple lines. If your docstring does extend over multiple lines, the closing three quotation marks must be on a line by itself, preferably preceded by a blank line.

numpydoc_example.foo(var1, var2, *args, long_var_name='hi', **kwargs)[source]

Summarize the function in one line.

Several sentences providing an extended description. Refer to variables using back-ticks, e.g. var.

Parameters:
  • var1 (array_like) – Array_like means all those objects – lists, nested lists, etc. – that can be converted to an array. We can also refer to variables like var1.

  • var2 (int) – The type above can either refer to an actual Python type (e.g. int), or describe the type of the variable in more detail, e.g. (N,) ndarray or array_like.

  • *args (iterable) – Other arguments.

  • long_var_name ({'hi', 'ho'}, optional) – Choices in brackets, default first when optional.

  • **kwargs (dict) – Keyword arguments.

  • only_seldom_used_keywords (type) – Explanation.

  • common_parameters_listed_above (type) – Explanation.

Returns:

  • type – Explanation of anonymous return value of type type.

  • describe (type) – Explanation of return value named describe.

  • out (type) – Explanation of out.

  • type_without_description

Raises:

BadException – Because you shouldn’t have done that.

See also

numpy.array

Relationship (optional).

numpy.ndarray

Relationship (optional), which could be fairly long, in which case the line wraps here.

numpy.dot, numpy.linalg.norm, numpy.eye

Notes

Notes about the implementation algorithm (if needed).

This can have multiple paragraphs.

You may include some math:

\[X(e^{j\omega } ) = x(n)e^{ - j\omega n}\]

And even use a Greek symbol like \(\omega\) inline.

References

Cite the relevant literature, e.g. [1]. You may also cite these references in the notes section above.

Examples

These are written in doctest format, and should illustrate how to use the function.

>>> a = [1, 2, 3]
>>> print([x + 3 for x in a])
[4, 5, 6]
>>> print("a\nb")
a
b