2023-03-10

Python Docstring Style

What is a Docstring

In Python, a docstring is a string literals that appears as the first statement in a module, function, class, or method definition. The purpose of a docstring is to provide documentation about the purpose, behavior, and usage of the code that follows it. A docstring can be a single-line or multi-line string, and it can be written using single or double quotes.

Why are Docstrings Important

Here are some reasons why docstrings are important in Python:

  • Clarity and Readability
    Docstrings help to make code more readable and understandable by providing clear explanations of what the code does and how to use it. By reading a docstring, a developer can quickly understand the purpose of a function, class, or module, and how it can be used in their own code.

  • Documentation
    Docstrings serve as a way to document code, making it easier for other developers to understand how it works and how to use it. This is particularly important when working on large projects, where multiple developers may be working on the same codebase.

  • Maintenance and Refactoring
    Docstrings can be useful when it comes to maintaining and refactoring code. They provide a reference for what the code does, making it easier to identify what needs to be changed and how to do it.

  • Testing and Debugging
    Docstrings can be used to test and debug code. By providing clear explanations of what the code is supposed to do, developers can ensure that their code is working as expected.

Docstring Formats

In Python, there are several different formats for writing docstrings. The format you choose will depend on your personal preference, as well as any guidelines or conventions you are following, such as those set out in the Python standard library or in third-party style guides. Here are some of the most common docstring formats in Python:

reStructuredText (reST) Format

reStructuredText (reST) is a lightweight markup language used for creating documentation in Python, as well as in other programming languages. It is designed to be easy to read and write, and to provide a flexible and extensible format for creating documentation.

Here is an example of a Python docstring in reST format:

python
def my_function(arg1, arg2):
    """
    Brief description of the function.

    A longer description that goes into more detail about the function and how it works.

    :param arg1: Description of arg1.
    :type arg1: int
    :param arg2: Description of arg2.
    :type arg2: str
    :return: Description of the return value.
    :rtype: bool
    :raises ValueError: If the function encounters a value error.
    """
    # function code goes here

In this example, the docstring begins with a brief summary of what the function does, followed by a longer description that provides more detail. The parameters are documented using the :param and :type directives, and the return value is documented using the :return and :rtype directives. The :raises directive is used to document any exceptions that the function may raise.

reST format is highly flexible and extensible, and can be used to create a wide range of documentation formats, including HTML, LaTeX, and PDF. It is also easy to learn and use, which makes it a popular choice for Python developers.

Google-Style Format

Google-style format is a popular format for docstrings used by Google and many other open-source projects. It is designed to be simple and easy to read, with a focus on clarity and consistency.

Here is an example of a Python docstring in Google-style format:

python
def my_function(arg1, arg2):
    """
    Brief description of the function.

    A longer description that goes into more detail about the function and how it works.

    Args:
        arg1 (int): Description of arg1.
        arg2 (str): Description of arg2.

    Returns:
        bool: Description of the return value.

    Raises:
        ValueError: If the function encounters a value error.
    """
    # function code goes here

In this example, the docstring begins with a brief summary of what the function does, followed by a longer description that provides more detail. The Args section includes a description of each parameter, as well as its type. The Returns section includes a description of what the function returns, as well as its type. The Raises section includes any exceptions that the function may raise.

Google-style format is easy to read and understand, and can be helpful for other developers who are using your code. It is also relatively concise, which can be helpful when documenting complex functions or libraries.

NumPy-Style Format

NumPy-style format is a format for docstrings used by the NumPy library and many other scientific Python packages. It is designed to be consistent with the NumPy documentation style and provides a clear and structured way to document scientific functions and libraries.

Here is an example of a Python docstring in NumPy-style format:

python
def my_function(arg1, arg2):
    """
    Brief description of the function.

    Parameters
    ----------
    arg1 : int
        Description of arg1.
    arg2 : str
        Description of arg2.

    Returns
    -------
    bool
        Description of the return value.

    Raises
    ------
    ValueError
        If the function encounters a value error.
    """
    # function code goes here

In this example, the docstring begins with a brief summary of what the function does, followed by a Parameters section that includes a description of each parameter, as well as its type. The Returns section includes a description of what the function returns, as well as its type. The Raises section includes any exceptions that the function may raise.

NumPy-style format is particularly useful for scientific functions and libraries, as it provides a consistent way to document complex algorithms and calculations. It also allows for the use of mathematical notation and formatting, which can be helpful when documenting scientific code.

References

https://peps.python.org/pep-0257/
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
https://betterprogramming.pub/3-different-docstring-formats-for-python-d27be81e0d68
https://google.github.io/styleguide/pyguide.html

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!