Since docstrings are free-form, it really depends on what you use to parse code to generate API documentation.

I would recommend getting familiar with the Sphinx markup, since it is widely used and is becoming the de-facto standard for documenting Python projects, in part because of the excellent readthedocs.org service. To paraphrase an example from the Sphinx documentation as a Python snippet:

def send_message(sender, recipient, message_body, priority=1) -> int:
   """
   Send a message to a recipient.

   :param str sender: The person sending the message
   :param str recipient: The recipient of the message
   :param str message_body: The body of the message
   :param priority: The priority of the message, can be a number 1-5
   :type priority: integer or None
   :return: the message id
   :rtype: int
   :raises ValueError: if the message_body exceeds 160 characters
   :raises TypeError: if the message_body is not a basestring
   """

This markup supports cross-referencing between documents and more. Note that the Sphinx documentation uses (e.g.) :py:attr: whereas you can just use :attr: when documenting from the source code.

Naturally, there are other tools to document APIs. There's the more classic Doxygen which uses \param commands but those are not specifically designed to document Python code like Sphinx is.

Note that there is a similar question with a similar answer in here...

Answer from anarcat on Stack Overflow
Top answer
1 of 9
317

Since docstrings are free-form, it really depends on what you use to parse code to generate API documentation.

I would recommend getting familiar with the Sphinx markup, since it is widely used and is becoming the de-facto standard for documenting Python projects, in part because of the excellent readthedocs.org service. To paraphrase an example from the Sphinx documentation as a Python snippet:

def send_message(sender, recipient, message_body, priority=1) -> int:
   """
   Send a message to a recipient.

   :param str sender: The person sending the message
   :param str recipient: The recipient of the message
   :param str message_body: The body of the message
   :param priority: The priority of the message, can be a number 1-5
   :type priority: integer or None
   :return: the message id
   :rtype: int
   :raises ValueError: if the message_body exceeds 160 characters
   :raises TypeError: if the message_body is not a basestring
   """

This markup supports cross-referencing between documents and more. Note that the Sphinx documentation uses (e.g.) :py:attr: whereas you can just use :attr: when documenting from the source code.

Naturally, there are other tools to document APIs. There's the more classic Doxygen which uses \param commands but those are not specifically designed to document Python code like Sphinx is.

Note that there is a similar question with a similar answer in here...

2 of 9
132

Based on my experience, the numpy docstring conventions (PEP257 superset) are the most widely-spread followed conventions that are also supported by tools, such as Sphinx.

One example:

Parameters
----------
x : type
    Description of parameter `x`.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - You can also retrieve this documentation using Python's help() function: ... Help on function square in module __main__: square(a) Returns the square of the given number. Multi-line Docstrings also contain the same string literals line as in One-line Docstrings, but it is followed by a single blank along with the descriptive text. The general format for writing a Multi-line Docstring is as follows: def some_function(argument1): """Summary or Description of the Function Parameters: argument1 (int): Description of arg1 Returns: int:Returning value """ return argument1 print(some_function.__doc__)
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ docstrings
Python Docstrings (With Examples)
The docstring for a function or method should summarize its behavior and document its arguments and return values.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ is this docstring correct ? should 'args' or 'parameters', can i avoid have empty line with special character?
r/learnpython on Reddit: Is this docstring correct ? should 'Args' or 'Parameters', can I avoid have empty line with special character?
October 5, 2022 -

Hi, can you please give me your feedback about the way I am writing Python docstring ?Mine looks usually like this: https://imgur.com/a/V6QJLJpand in full text (as I cannot insert the image directly):

def getItemsByFolder(folderId):
"""Get all Items from database that belong to a particular folder.

Args:
folderId (str): A valid folder Id that is present in the Items database
Returns:list: A list containing folders folder Items represented as dictionaries"""

  1. Is there a possibility to avoid this empty line above 'Args' ? (with a special character for example)I do not like it because it takes a lot of screen real-estate for nothing it makes functions hard to read.

  2. Is 'Args' or 'Parameters' the correct keyword ?

๐ŸŒ
Python
peps.python.org โ€บ pep-0257
PEP 257 โ€“ Docstring Conventions | peps.python.org
The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated.
๐ŸŒ
Readthedocs
sphinxcontrib-napoleon.readthedocs.io โ€บ en โ€บ latest โ€บ example_google.html
Example Google Style Python Docstrings โ€” napoleon 0.7 documentation
If attribute, parameter, and return types are annotated according to `PEP 484`_, they do not need to be included in the docstring: Args: param1 (int): The first parameter. param2 (str): The second parameter. Returns: bool: The return value. True for success, False otherwise. .. _PEP 484: https://www.python.org/dev/peps/pep-0484/ """ def function_with_pep484_type_annotations(param1: int, param2: str) -> bool: """Example function with PEP 484 type annotations.
๐ŸŒ
Python
python.org โ€บ dev โ€บ peps โ€บ pep-0257
PEP 257 -- Docstring Conventions | Python.org
May 29, 2001 - The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated.
Find elsewhere
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ python โ€บ python docstring: documenting and introspecting functions
Python Docstring: Documenting And Introspecting Functions
April 1, 2025 - co_argcount counts 2 because it doesnโ€™t consider any argument variable prefixed with * or **. ... Answer: In Python, type hints donโ€™t do much by themselves. They are mostly used to inform the reader of the type of code a variable is expected to be. The good news is that its information can be used to implement type checks. This is commonly done in Python decorators. ... Answer: A docstring is the first string literal enclosed in triple-double quotes (โ€œโ€โ€), and immediately follows a class, module, or functionโ€™s definition.
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ pycharm โ€บ using-docstrings-to-specify-types.html
Specify types with docstrings | PyCharm Documentation
September 1, 2025 - You can also document what a function returns. PyCharm will generate a :return: and :rtype: section (or their equivalent in the selected docstring format).
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python function docstrings
Python Function Docstrings
March 26, 2025 - When the first line in the function body is a string, Python will interpret it as a docstring. For example: def add(a, b): "Return the sum of two arguments" return a + bCode language: Python (python)
๐ŸŒ
Lftechnology
coding-guidelines.lftechnology.com โ€บ docstrings
Convention for docstrings | Leapfrog Coding Guidelines
If attribute, parameter, and return types are annotated according to `PEP 484`_, they do not need to be included in the docstring: Args: param1 (int): The first parameter. param2 (str): The second parameter. Returns: bool: The return value. True for success, False otherwise. .. _PEP 484: https://www.python.org/dev/peps/pep-0484/ """ def function_with_pep484_type_annotations(param1: int, param2: str) -> bool: """Example function with PEP 484 type annotations.
๐ŸŒ
Python-sprints
python-sprints.github.io โ€บ pandas โ€บ guide โ€บ pandas_docstring.html
pandas docstring guide โ€” Python documentation
Video tutorial: Pandas docstring guide by Frank Akogun. A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation.
๐ŸŒ
Lsst
developer.lsst.io โ€บ python โ€บ numpydoc.html
Documenting Python APIs with docstrings โ€” LSST DM Developer Guide main documentation
Class docstrings are placed directly after the class definition, and serve to document both the class as a whole and the arguments passed to the __init__ constructor.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Each method docstring documents parameters and return values. Proper indentation ensures readability and compatibility with tools like help(). Comments (#): Explain code but are ignored by Python at runtime.
Top answer
1 of 2
1

Writing good documentation is an art unto itself. Repeating yourself (e.g., repeating the variable name without underscores) is valueless; e.g., ++i; // increment i. If you're just repeating variable names without underscores, then you're not adding to anyone's understanding of the code.

I think it's always important to document what exceptions your methods throw. Return values aren't always as obvious as the original programmer thinks they are, so that's usually useful to document as well. (Again, if your method is get_foo(), saying "returns a foo" doesn't add anything.)

One thing to point out about that mypy example, the authors are using type hints which replaces a lot of what would be included in a docstring:

def warn(self, msg: str, context: Context, file: Optional[str] = None,
             origin: Optional[Context] = None)

Just from this, I know exactly what this method expects. Without it:

def warn(self, msg, context, file = None, origin = None)

I know a lot less.

2 of 2
3

I can not give you a definite answer, since such documentation also is sort of work and as such should be done, if useful. Generally speaking, only useful functionality should be exposed to the user and the application should be easy to use and understand.

Arguments Even obvious things can be exported for fast lookup. This is usually helpful for long and tedious methods. Optional parameters can be easily integrated by default parameters.
Return values With python it can be a pain to look up return values by inspecting objects (without source code), so when the source code is not at hand this should be done.
Side effects This is a quite vague term, but at least thread-safety support should be annotated in mixed applications cases (or explicitely). Default however is not thread-safe.
Exceptions Enforcing programmer/user input should be definitely documented, since this will cause crashing behavior.

In a small project, and as such internal project, for given knowledge and (hopefully) code, it does not make too much sense to comment everything. Concentrate on data structures and use cases for functions.

๐ŸŒ
Readthedocs
numpydoc.readthedocs.io โ€บ en โ€บ latest โ€บ format.html
Style guide โ€” numpydoc v1.11.1.dev1+gca74aae44 Manual
Description of the function arguments, keywords and their respective types. Parameters ---------- x : type Description of parameter `x`. y Description of parameter `y` (with type not specified). The colon must be preceded by a space, or omitted if the type is absent. When referring to a parameter anywhere within the docstring, enclose its name in single backticks.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Python Docstring Formats (Styles) and Examples | note.nkmk.me
August 26, 2023 - Docstrings often contain descriptions and types of function arguments and return values. However, there is no standardized format for this. coding style - What are the most common Python docstring formats?
๐ŸŒ
Dataquest
dataquest.io โ€บ home โ€บ blog โ€บ how to use python docstrings for effective code documentation
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - I strongly recommend you read it all even though you may not understand all of it. The essential points are as follows: Use triple double quotes to enclose docstrings. Docstring ends with a dot.
๐ŸŒ
Sfriederichs
sfriederichs.github.io โ€บ how-to โ€บ python โ€บ dox โ€บ 2017 โ€บ 12 โ€บ 29 โ€บ Python-Docstrings.html
How To Document Python Code With Docstrings
""" Python Docstring Example v0.1 Author: Stephen Friederichs This script demonstrates the use of docstrings versus conventional Python comments. It also has other wicked awesome things going on like automatic text wrapping and command-line argument handling The following command-line parameters control the behavior of the script: -h, --help - Shows this screen and exits -a, --functiona - Shows the docstring for *funcA* -b, --functionb - Shows the docstring for *funcB* """ import textwrap import getopt import sys def prettyPrint(uglyString): """This function properly fomats docstrings for prin
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ python-docstring
Python Docstring - AskPython
February 16, 2023 - Python docstring is surrounded by a pair of triple double-quotes (โ€œโ€โ€). Letโ€™s look at some examples of writing docstrings in Python. def multiply(a, b): """This method multiplies the given two numbers. Input Arguments: a, b must be numbers. ...