How to write Python document comments (Docstrings)

Python doc comment

How to write Python document comments (Docstrings). Hi, a faction? school? There seems to be.

  1. NumPy style
  2. Google Style

Short Summary: A brief one-line description. Name

NumPy style.py


def add(a, b):
   """The sum of two numbers.

   """

Google style.py


def add(a, b):
   """The sum of two numbers.

   """

Extended Summary: A detailed summary of multiple lines

NumPy style.py


def add(a, b):
   """The sum of two numbers.

   (Extended Summary)
   """

Google style.py


def add(a, b):
   """The sum of two numbers.

   (Extended Summary)
   """

Parameters: Function arguments

NumPy style.py


"""
Parameters
----------
x : type
    Description of parameter `x`.
y
    Description of parameter `y` (with type not specified)
"""

Google style.py


"""
Args:
  x (type): Description of parameter `x`.
  y: Description of parameter `y` (with type not specified)
"""

Returns: Function returns

NumPy style.py


"""
Returns
-------
int
    Description of anonymous integer return value.
"""

NumPy style.py


"""
Returns
-------
err_code : int
    Non-zero value indicates error code, or zero on success.
err_msg : str or None
    Human readable error message, or None on success.
"""

Google style.py


"""
Returns:
  int: Description of anonymous integer return value.
"""

Raises: Exception

NumPy style.py


"""
Raises
------
LinAlgException
    If the matrix is not numerically invertible.
"""

Google style.py


"""
Raises:
  LinAlgException: If the matrix is not numerically invertible.
"""

See Also: Related items

NumPy style.py


"""
See Also
--------
average : Weighted average
"""

Google style.py


#None

Notes: Remarks. Additional description. You can also use LaTeX.

NumPy style.py


"""
Notes
----------
The FFT is a fast implementation of the discrete Fourier transform:
.. math:: X(e^{j\omega } ) = x(n)e^{ - j\omega n}
"""

Google style.py


"""
Note:
  The FFT is a fast implementation of the discrete Fourier transform:
  .. math:: X(e^{j\omega } ) = x(n)e^{ - j\omega n}
"""

Examples: Usage examples

NumPy style.py


"""
Examples
----------
>>> np.add(1, 2)
3
"""

Google style.py


"""
Example:
  >>> np.add(1, 2)
  3
"""

Attributes: Class attributes

NumPy style.py


"""
Attributes
----------
x : float
    The X coordinate.
y : float
    The Y coordinate.
"""

Google style.py


"""
Attributes:
  x (float): The X coordinate.
  y (float): The Y coordinate.
"""

Methods: methods of the class

NumPy style.py


"""
Methods
-------
colorspace(c='rgb')
    Represent the photo in the given colorspace.
gamma(n=1.0)
    Change the photo's gamma exposure.
"""

Google style.py


#None

Recommended Posts

How to write Python document comments (Docstrings)
How to write a Python class
How to write Ruby to_s in Python
How to write pydoc and multi-line comments
How to install Python
How to install python
How to write a named tuple document in 2020
20th Offline Real-time How to Write Problems in Python
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
How to write string concatenation in multiple lines in Python
XPath Basics (2) -How to write XPath
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to write a list / dictionary type of Python3
How to use Python argparse
Python: How to use pydub
[Python] How to write a docstring that conforms to PEP8
[Python] How to use checkio
How to run Notepad ++ Python
Write to csv with Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Introduction to Python] How to write repetitive statements using for statements
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Introduction to Python] How to write conditional branches using if statements
Offline real-time how to write Python implementation example of E14
[Python] How to write an if statement in one sentence.
How to write a metaclass that supports both python2 and python3
How to write offline real-time Solving E05 problems with Python
How to install python using anaconda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
[Python] Write to csv file with Python
[Python] How to derive nCk (ABC156-D)
How to write soberly in pandas
[Python] How to use Pandas Series
How to collect images in Python
Flask reuse How to write html
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to get the Python version
How to get started with Python
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
[Python] How to swap array values
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python