Skip to content

Write Python Docstrings: Guide to Documenting Functions

Updated: at 03:23 AM

Docstrings are string literals that appear right after the definition of a function, method, class, or module in Python. They are used to document these elements and explain their intended purpose and usage. Well-written docstrings improve the readability and usability of Python code.

Docstrings serve as documentation that programmers can conveniently access. They provide a quick way to understand what a function does without having to read through the actual code. Docstrings are also used by automated documentation generation tools like Sphinx to create API documentation. This allows users to learn how to use various modules, classes, and functions without needing access to the source code.

In this comprehensive guide, we will cover the following topics:

Table of Contents

Open Table of Contents

Purpose and Benefits of Docstrings

Here are some of the main reasons why you should write docstrings in Python:

Self-documenting code - Docstrings embedded in the code provide documentation accessible to programmers without needing to look elsewhere. This makes the code easier to understand and use.

Code readability - Concise, well-written docstrings can summarize complex code, making it more readable.

User documentation - Docstrings serve as API documentation for users and programmers looking to understand your code and use it properly.

Automated documentation - Tools like Sphinx scrape docstrings to automatically generate HTML/PDF documentation for projects.

Code linting - Linters like Pylint can check docstrings to ensure code is properly documented.

Test documentation - Docstrings provide useful context and details for testing code and understanding expected behavior.

In summary, comprehensive docstrings make code more readable, maintainable and user-friendly. The documentation stays up to date with the code without requiring extra effort.

Docstring Conventions and Best Practices

There are some standard conventions and best practices to follow when writing effective Python docstrings:

Be concise yet descriptive - Stick to the essence but provide sufficient details on usage and purpose. Verbose docstrings are ignored.

Use triple double quotes - Docstrings are surrounded by triple quotes rather than single quotes.

"""This is a multi-line docstring."""

Follow a standard format- PEP 257 convention provides guidelines on formatting docstrings with uniformity.

Document all public interfaces - Provide a docstring for any function, class, or module that is part of the public API.

Describe parameters and return values - List each parameter with description. Explain return values and type if applicable.

Include examples and common use cases - Provide concrete examples of how the code is used.

Use proper grammar and punctuation - Treat the docstring like proper technical documentation with correct spelling and formatting.

Keep docstrings up-to-date - Update docstrings whenever code changes to ensure accuracy.

These conventions will ensure you generate proper documentation that remains useful over time.

Docstring Formats

There are several commonly used formats for structuring Python docstrings:

PEP 257 Standard Format

PEP 257 introduced guidelines for docstring formatting that are considered the standard convention in Python. The format structure is:

"""One-line summary of function/module/class.

Extended description providing additional details.

    param1 (int): Description of parameter param1
    param2 (str): Description of parameter param2

    int: Description of return value

    Errors: Description of error conditions raised

It starts with a single line summarizing the code element, followed by a blank line and detailed description. Parameters, return values, and exceptions raised are also documented.

reStructuredText Format

reStructuredText is a commonly used lightweight markup language for documentation. It can be parsed to generate rich documentation in HTML/PDF formats.

reStructuredText docstrings in Python look like:

.. function:: countdown(n)
   :n: int

Generator function that counts down from n to 0.

:param n: The number to start counting down from.
:type n: int
:returns: A generator that yields the next number in the countdown on each iteration.
:rtype: generator

reStructuredText provides sections, roles like :param: and :returns:, inline markup for code samples, lists, warnings etc. This allows for very rich and structured documentation if needed.

Numpydoc Format

Numpydoc is a docstring standard used extensively in NumPy, SciPy and other scientific Python projects. It builds upon reStructuredText conventions.


Generator function that counts down from n to 0.

n : int
    The number to start counting down from.

    A generator that yields the next number in the countdown on each iteration.

>>> for i in countdown(10):
>>>     print(i)

The Parameters and Returns sections provide consistent types and descriptions for arguments and return values. It also allows for an Examples section with actual code samples.

Tools for Testing and Validating Docstrings

Here are some useful tools you can use to validate docstring formatting and check code documentation coverage in Python:

These tools will help you validate docstring content, uncover undocumented code, and fix formatting issues. They are useful both in development and as part of continuous integration pipelines.

Real World Example and Usage

To demonstrate real-world usage, let’s look at an example module that provides a countdown generator function:

"""Library implementing countdown timer functionalities.

The countdown module provides a `countdown()` generator that can be used to
iterate over a countdown sequence starting from a provided number.

Typical usage:

    >>> from countdown import countdown
    >>> for i in countdown(5):
    >>>     print(i)

def countdown(n):
    """Generator that counts down from `n` to 0.

      n (int): Number to count down from.

      int: Next number in the countdown sequence, from `n` to 0.

        >>> for i in countdown(5):
        >>>     print(i)
    while n > 0:
        yield n
        n -= 1

The module docstring at the top describes what the module provides. The function countdown() is documented with a summary line, followed by a description of the generator’s behavior.

The Args, Yields, and Examples sections give details on parameters, returned values, and example usage respectively. Someone just reading the docstring can understand how to use this function without reading the code.

Tools like Sphinx and pydoc can directly generate API documentation for this module using its thorough docstrings. The code is also more maintainable long-term since the documentation stays up to date and developers can understand behavior just by reading docstrings.


Well-documented code makes Python libraries, modules, and functions much easier to use and maintain. Writting clear, informative docstrings that follow PEP 257 guidelines is essential for producing high-quality documentation. The right tools can help validate docstring formatting and analyze documentation coverage. Following the conventions and best practices outlined in this guide will help you improve the readability, usability, and maintainability of your Python code through comprehensive documentation.