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
🌐
Python
docs.python.org › 3 › builtins › functions.html
Built-in Functions — Python 3.14.7 documentation
See the integer string conversion length limitation documentation. Changed in version 3.14: int() no longer delegates to the __trunc__() method. ... Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtual) subclass thereof. If object is not an object of the given type, the function always returns False.
🌐
Real Python
realpython.com › documenting-python-code
Documenting Python Code: A Complete Guide – Real Python
July 17, 2026 - In this section, you’ll learn about docstrings and how to use them for documentation. This section is further divided into the following sub-sections: Docstrings Background: A background on how docstrings work internally within Python · Docstring Types: The various docstring “types” (function, class, class method, module, package, and script)
Discussions

python - How to document a method with parameter(s)? - Stack Overflow
One more example here, with some tiny details documented inline: Copydef foo( # Note that how I use the parenthesis rather than backslash "\" # to natually break the function definition into multiple lines. a_very_long_parameter_name, # The "inline" text does not really have to be at same line, # when your parameter name is very long. # Besides, you can use this way to have multiple lines doc too. # The one extra level indentation here natually matches the # original Python ... More on stackoverflow.com
🌐 stackoverflow.com
What are your preferred conventions for documenting python code?
Module docstrings. I don't think I've ever worked with someone else who writes them. Open up a file for the first time, see 30 lines of imports and a bunch of functions that call each other, the file name is generic and provides no context. This tier of missing documentation is really painful. Just explain what should be used outside the module, specific terminology used here and how things link together. I'm tired of building up the design in my mind from all the implementation bits. More on reddit.com
🌐 r/Python
20
22
December 7, 2022
Is there a program to help you document Python functions?
I use this extension in VS Code More on reddit.com
🌐 r/Python
56
247
September 17, 2020
design - How should I document a higher-order Python function? - Software Engineering Stack Exchange
I'm interested in designing a high level function in Python, (I.E. a function that takes other functions as arguments) but I don't know of any good conventions for documenting such a function. For More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Docstrings (Documentation Strings) are special strings used to document Python code. They provide a description of what a module, class, function or method does.
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`.
🌐
Lsst
developer.lsst.io › v › DM-7674 › docs › py_docs.html
Documenting Python APIs — LSST DM Developer Guide latest documentation
Parameters ---------- values : iterable Python iterable whose values are summed. Returns ------- sum : `float` Sum of `values`. """ pass · Like method and function docstrings, the docstring should immediately follow the class definition, without a blank space.
Find elsewhere
🌐
Python How Tos
campbell-muscle-lab.github.io › howtos_Python › pages › documentation › best_practices › best_practices.html
Documentation Best Practices - Python How Tos
The Parameters section is very important and should be included any time parameters are passed to the function. Document all parameters in this section with the following format:
🌐
Python Basics
python-basics-tutorial.readthedocs.io › en › latest › functions › index.html
Functions - Python Basics
Basic function definitions: The basic syntax for a Python function definition is As with control streams, Python uses indentation to separate the function from the function definition. The followin...
🌐
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.
🌐
YouTube
youtube.com › watch
Write Python Docstrings Effectively: Understanding & Accessing Docstrings - YouTube
Download your free Python Cheat Sheet here: https://realpython.com/cheatsheetFree Python Skill Test with instant level + learning plan: https://realpython.co...
Published: February 19, 2026
🌐
Medium
medium.com › @syedar.sohail › docstring-and-why-is-it-important-python-classes-modules-and-functions-95fee5247ff5
Docstring and why is it important ? — Python Classes, Modules and Functions | by Sohail | Medium
October 30, 2022 - Please do not confuse thinking comments and Docstrings are the same. well, they may highly look similar in the way they work but there is a lot of difference. Comments are written generally to show some unusual portions of code and for fixing the bugs. While Docstrings are the right tool for documenting the classes, functions, modules and packages.
🌐
Lsst
developer.lsst.io › v › DM-5063 › docs › py_docs.html
Documenting Python Code — LSST DM Developer Guide latest documentation
A one-line summary that does not use variable names or the function name: def add(a, b): """Sum two numbers.""" return a + b · The summary should be written as a present-tense action. Do not write something like “Sums two numbers.” · The one line summary can be used alone only in extremely trivial cases, such as Python properties.
🌐
W3Schools
w3schools.com › python › python_functions.asp
Python Functions
A function helps avoiding code repetition. In Python, a function is defined using the def keyword, followed by a function name and parentheses:
🌐
Python
docs.python.org › 3
Python 3.14 documentation
4 weeks ago - What's new in Python 3.14? Or all "What's new" documents since Python 2.0 · Tutorial Start here: a tour of Python's syntax and features · Built-ins reference Built-in functions and classes · Library reference Standard library modules · Language reference Syntax and language elements ·
🌐
Reddit
reddit.com › r/python › is there a program to help you document python functions?
r/Python on Reddit: Is there a program to help you document Python functions?
September 17, 2020 -

I just finished a Python project and now want to go back and document each function while it is still fresh in my head. Is there a program that will scan your code for functions that don't have a doc-string and then interactively ask you to:

  • describe the function

  • document each parameter

  • document the return value

It would then create a doc-string for you that you could simply copy/paste into your code.

Thanks.

🌐
Python
python.org › doc
Our Documentation | Python.org
Open source software is made better when users can easily contribute code and documentation to fix bugs and add features. Python strongly encourages community involvement in improving the software.
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
Write docstrings for all public modules, functions, classes, and methods. Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.7 documentation
For brevity, it should not explicitly state the object’s name or type, since these are available by other means (except if the name happens to be a verb describing a function’s operation). This line should begin with a capital letter and end with a period. If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the object’s calling conventions, its side effects, etc. The Python parser strips indentation from multi-line string literals when they serve as module, class, or function docstrings.
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › writing › documentation
Documentation — The Hitchhiker's Guide to Python
Extended description of function. Parameters ---------- arg1 : int Description of arg1 arg2 : str Description of arg2 Returns ------- int Description of return value """ return 42 · The sphinx.ext.napoleon plugin allows Sphinx to parse this style of docstrings, making it easy to incorporate NumPy style docstrings into your project. At the end of the day, it doesn’t really matter what style is used for writing docstrings; their purpose is to serve as documentation for anyone who may need to read or make changes to your code.