PEP 257 – Docstring Conventions | peps.python.org



Introduction

Python docstrings are a type of documentation that are used to describe the functionality of a module, function, class, or method within Python code. Docstrings are used to document the following:

Docstrings are written as comments in Python code and are enclosed in triple quotes. For example:

def my_function(arg1, arg2):
    """
    This function takes two arguments, arg1 and arg2, and returns their sum.
    """
    return arg1 + arg2

Types of Docstrings

There are two main types of docstrings in Python: one-line docstrings and multi-line docstrings.

One-Line Docstrings

One-line docstrings are used to document the function signature, which includes the function name, arguments, and the return value. They are written as a single line enclosed in triple quotes. For example:

def my_function(arg1, arg2):
    """Return the sum of arg1 and arg2."""
    return arg1 + arg2