Python
docstring
formats
coding-standards
documentation

What are the most common Python docstring formats?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Python docstrings are an essential tool for documenting your code, particularly functions, classes, and modules. They provide a way to describe what a function or class does, its parameters, return values, exceptions, and more. Several common formats have emerged for writing docstrings, each with its style and conventions. This article explores these popular formats—with examples—and offers a summary in a neatly organized table to help you choose the best format for your project.

Understanding Python Docstrings

Docstrings in Python are string literals that occur as the first statement in a module, function, class, or method definition. They're typically used for generating automatic documentation or simply explaining the code's purpose and usage to other developers or future you.

Common Docstring Formats

Here we will dive into the most popular Python docstring formats: reStructuredText, NumPy/SciPy, and Google format. Each of these has its own conventions and is better suited for different situations and personal preferences.

1. reStructuredText (reST)

reStructuredText is a markup syntax used in Python's built-in documentation generation tool, Sphinx. It's very popular for open-source Python project documentation.

Example of a function docstring in reST format:

python
1def example_function(param1, param2):
2    """
3    This is a short description of the function.
4
5    :param param1: Description of param1.
6    :type param1: int
7    :param param2: Description of param2.
8    :type param2: str
9    :return: Description of return value.
10    :rtype: bool
11    :raises ValueError: If `param1` is negative.
12    """
13    pass

2. NumPy/SciPy

The NumPy/SciPy format is widely used in scientific and mathematical Python packages. It starts with a brief summary line followed by an elaborated description.

Example of a function docstring in NumPy/SciPy format:

python
1def example_function(param1, param2):
2    """
3    Short summary of the function.
4
5    Extended description of the function.
6
7    Parameters
8    ----------
9    param1 : int
10        Description of param1.
11    param2 : str
12        Description of param2.
13
14    Returns
15    -------
16    bool
17        Description of return value.
18
19    Raises
20    ------
21    ValueError
22        If `param1` is negative.
23    """
24    pass

3. Google Style

The Google style is well-regarded for its simplicity and readability. It is often preferred in organizations that develop large-scale applications.

Example of a function docstring in Google style:

python
1def example_function(param1, param2):
2    """
3    A brief summary of the function.
4
5    Args:
6        param1 (int): Description of param1.
7        param2 (str): Description of param2.
8
9    Returns:
10        bool: Description of return value.
11
12    Raises:
13        ValueError: If `param1` is negative.
14    """
15    pass

Comparing Docstring Formats

To help you decide which docstring format suits you best, consider the following table that summarizes the different formats:

FeaturereStructuredTextNumPy/SciPyGoogle Style
SummaryDetailed options for project documentationStrictly structured for scientific documentationClear and concise for general use
Parameter Formatting:param type name:name : typename (type):
Return Type:rtype:Returns blockReturns:
PopularityWidely used with SphinxCommon in academiaGaining popularity across sectors
Ease of UseFlexible but complexStructured but detailedSimple and readable

Additional Details to Enhance Understanding

Inline Documentation Tools

Other tools, such as docstring analyzers and formatters, can help ensure your docstrings are consistent with the chosen format. For example, the pydocstyle tool can check docstring adherence to conventions.

Why Use Docstrings?

  • Self-Documentation: Your code becomes self-explanatory, reducing the need for external reference.
  • API Documentation: Tools like Sphinx can automatically generate professional-looking API documentation from docstrings.
  • Code Quality: Encourages developers to think critically about parameter types, return values, and edge cases.

Choosing the correct docstring format often comes down to personal preference or organizational guidelines. Each format has its strengths and weaknesses, but all aim to make your code more understandable and accessible to humans and machines. Whether you're contributing to open-source projects, building enterprise applications, or dabbling in data science, well-written docstrings will always be a valuable asset.


Course illustration
Course illustration

All Rights Reserved.