Formats

Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post.

Note that the reST is recommended by the PEP 287

There follows the main used formats for docstrings.

- Epytext

Historically a javadoc like style was prevalent, so it was taken as a base for Epydoc (with the called Epytext format) to generate documentation.

Example:

"""
This is a javadoc style.

@param param1: this is a first param
@param param2: this is a second param
@return: this is a description of what is returned
@raise keyError: raises an exception
"""

- reST

Nowadays, the probably more prevalent format is the reStructuredText (reST) format that is used by Sphinx to generate documentation. Note: it is used by default in JetBrains PyCharm (type triple quotes after defining a method and hit enter). It is also used by default as output format in Pyment.

Example:

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

- Google

Google has their own format that is often used. It also can be interpreted by Sphinx (ie. using Napoleon plugin).

Example:

"""
This is an example of Google style.

Args:
    param1: This is the first param.
    param2: This is a second param.

Returns:
    This is a description of what is returned.

Raises:
    KeyError: Raises an exception.
"""

Even more examples

- Numpydoc

Note that Numpy recommend to follow their own numpydoc based on Google format and usable by Sphinx.

"""
My numpydoc description of a kind
of very exhautive numpydoc format docstring.

Parameters
----------
first : array_like
    the 1st param name `first`
second :
    the 2nd param
third : {'value', 'other'}, optional
    the 3rd param, by default 'value'

Returns
-------
string
    a value in a string

Raises
------
KeyError
    when a key error
OtherError
    when an other error
"""

Converting/Generating

It is possible to use a tool like Pyment to automatically generate docstrings to a Python project not yet documented, or to convert existing docstrings (can be mixing several formats) from a format to an other one.

Note: The examples are taken from the Pyment documentation

Answer from daouzli on Stack Overflow
🌐
Real Python
realpython.com › ref › best-practices › docstrings
docstrings | Python Best Practices – Real Python
PEP 257 contains Python’s official conventions for docstrings and helps keep their structure predictable across tools and projects. When documenting your public API with docstrings, keep the following best practices in mind:
Discussions

python - Is it consider bad practice to formally document implementation code? - Software Engineering Stack Exchange
However, most python code-bases ... omitting docstrings for implementation classes/functions/modules. With that in mind, I'm wondering if I should avoid doing this in the future as well. It seems that if other python devs are not doing it, there are probably good reasons why. Would it be bad practice to continue ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
May 13, 2017
Propper way to write DocStrings
There's this https://peps.python.org/pep-0257/ More on reddit.com
🌐 r/learnpython
5
1
January 5, 2023
Docstring vs Comments
Docstrings are easily obtainable by other Python tools dynamically just by inspecting your objects. This is useful for tools that do things like generating API documentation. Comments are, by comparison, more difficult for such tools to use in part because comments are discarded by the compiler whereas docstrings are a part of your object (see .__doc__ attribute of any function, class, etc.). They're not necessarily interchangable tools, however. They're different tools for different purposes. Also, docstrings only work in certain places like at the very beginning of modules, classes, or functions. Comments, on the other hand, can be placed anywhere. More on reddit.com
🌐 r/learnpython
4
8
September 11, 2024
What's the best guide/model to writing good docstrings for modules/classes/methods?
Check out this stack overflow answer . I won't copy paste the good info from that, but I would highlight this: Note that the reST is recommended by the PEP 287 And... Nowadays, the probably more prevalent format is the reStructuredText (reST) format In my experience, this is the case. More on reddit.com
🌐 r/learnpython
3
14
August 27, 2017
Top answer
1 of 4
1389

Formats

Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post.

Note that the reST is recommended by the PEP 287

There follows the main used formats for docstrings.

- Epytext

Historically a javadoc like style was prevalent, so it was taken as a base for Epydoc (with the called Epytext format) to generate documentation.

Example:

"""
This is a javadoc style.

@param param1: this is a first param
@param param2: this is a second param
@return: this is a description of what is returned
@raise keyError: raises an exception
"""

- reST

Nowadays, the probably more prevalent format is the reStructuredText (reST) format that is used by Sphinx to generate documentation. Note: it is used by default in JetBrains PyCharm (type triple quotes after defining a method and hit enter). It is also used by default as output format in Pyment.

Example:

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

- Google

Google has their own format that is often used. It also can be interpreted by Sphinx (ie. using Napoleon plugin).

Example:

"""
This is an example of Google style.

Args:
    param1: This is the first param.
    param2: This is a second param.

Returns:
    This is a description of what is returned.

Raises:
    KeyError: Raises an exception.
"""

Even more examples

- Numpydoc

Note that Numpy recommend to follow their own numpydoc based on Google format and usable by Sphinx.

"""
My numpydoc description of a kind
of very exhautive numpydoc format docstring.

Parameters
----------
first : array_like
    the 1st param name `first`
second :
    the 2nd param
third : {'value', 'other'}, optional
    the 3rd param, by default 'value'

Returns
-------
string
    a value in a string

Raises
------
KeyError
    when a key error
OtherError
    when an other error
"""

Converting/Generating

It is possible to use a tool like Pyment to automatically generate docstrings to a Python project not yet documented, or to convert existing docstrings (can be mixing several formats) from a format to an other one.

Note: The examples are taken from the Pyment documentation

2 of 4
354

The Google style guide contains an excellent Python style guide. It includes conventions for readable docstring syntax that offers better guidance than PEP-257. For example:

def square_root(n):
    """Calculate the square root of a number.

    Args:
        n: the number to get the square root of.
    Returns:
        the square root of n.
    Raises:
        TypeError: if n is not a number.
        ValueError: if n is negative.

    """
    pass

I like to extend this to also include type information in the arguments, as described in this Sphinx documentation tutorial. For example:

def add_value(self, value):
    """Add a new value.

       Args:
           value (str): the value to add.
    """
    pass
🌐
Real Python
realpython.com › documenting-python-code
Documenting Python Code: A Complete Guide – Real Python
July 17, 2026 - Python has one more feature that simplifies docstring creation. Instead of directly manipulating the __doc__ property, the strategic placement of the string literal directly below the object will automatically set the __doc__ value.
🌐
Google
google.github.io › styleguide › pyguide.html
Google Python Style Guide
The presence of a trailing comma ... element is present. Yes: golomb3 = [0, 1, 3] golomb4 = [ 0, 1, 4, 6, ] ... Two blank lines between top-level definitions, be they function or class definitions. One blank line between method definitions and between the docstring of a class ...
🌐
Medium
medium.com › geekculture › the-importance-of-writing-clear-docstrings-in-programming-62bffffa3881
Python best practices: Docstrings | by Ethan Jones | Geek Culture | Medium
February 8, 2024 - Helps write clear and consist code — it’s often easy to get carried away, so reviewing docstrings keys functions to the point. For this example, let’s look at 2 different versions of a Python function docstring…
Find elsewhere
🌐
Python
peps.python.org › pep-0257
PEP 257 – Docstring Conventions | peps.python.org
All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. Public methods (including the __init__ constructor) should also have docstrings.
Top answer
1 of 4
7

It is not necessarily bad practice to write doc strings for implementation code.
One thing to watch out for is that if the doc strings end up in official documentation, then other people might start to depend on internal details that you may want to be able to change at will. If that is a real concern for you, then you could also write the documentation in regular comments, rather than in a doc string.

Writing good documentation is hard and to most programmers (myself included) less fun than writing code. If you then add a bit of over-confidence on how readable your code is, then it becomes really easy to say "this internal function I just wrote is so clear, the code can stand on itself without additional documentation." The real proof of that statement usually comes several months later, when maintenance needs to be done and the code turns out to be less self-documenting than you thought.
It is very good on you if you can avoid that trap most of the time.

2 of 4
7
  1. Public methods are used within a larger scope than non-public ones, and by a broader range of persons.

  2. Non-public methods change more often than public ones (when the application is mature enough).

When it comes to comments, my first assertion means that it is much more important to document public methods. Those are the methods which would often be accessed by persons who don't necessarily have time (or interest) in exploring all the internals of the code: they just need to use the method, and they need to know how to use it. Inversely, those who will be interested by non-public methods are the persons who are often familiar with the class, and if not, they will have to become familiar with it, since they are modifying the class (otherwise, they wouldn't have to access non-public methods in the first place) or even the concerned method itself.

The second assertion means that it is costlier to keep the documentation of non-public methods up to date, especially when considering the ratio between public and non-public methods. If the method changes too often, it means that the documentation is read by fewer persons compared to the documentation of a public interface which remains the same for months or years.

To conclude, small-scope, non-public methods usually don't need as much documentation as public interfaces, and they are much more volatile. In other words, there is less money saved (in terms of future developers' time) by documenting a non-public method than a public one, and more money wasted constantly updating the documentation.

This explains PEP-8 guideline. One can imagine, obviously, some examples where it is absolutely crucial to document a non-public method, and examples where a public method is so self-explanatory, that it needs no comments. Those are the cases where that PEP-8 guideline should not be followed.

🌐
Josh Di Mella
joshdimella.com › blog › python-docstring-formats-best-practices
A Guide to Python Docstring Formats: Choosing the Right Style for Your Code | Josh Di Mella | Software Engineer
May 31, 2023 - Choosing the right docstring format is essential for maintaining consistent and readable documentation in your Python code. Whether you opt for Google style, Sphinx/reStructuredText, NumPy style, or Epytext, the key is to maintain consistency within your codebase or project.
🌐
Geo-python
geo-python.github.io › site › notebooks › L4 › writing-scripts.html
Good coding practices - Writing our scripts the “right” way — Geo-Python site documentation
In this example the script is simple, but many Python programs have optional values that can be used by the code when it is run, making the usage statement crucial. Note that the closing quotes are on a line by themselves. It is also possible to write one-line docstrings, for example with very simple functions, in which case the starting and closing quotation marks are on the same line.
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › comments, docstrings, and type hints in python code
Comments, Docstrings, and Type Hints in Python Code - MachineLearningMastery.com
June 21, 2022 - Because of the special status of the docstring, there are several conventions on how to write a proper one. In C++, we may use Doxygen to generate code documentation from comments, and similarly, we have Javadoc for Java code. The closest match in Python would be the tool “autodoc” from Sphinx or pdoc.
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.
🌐
Programiz
programiz.com › python-programming › docstrings
Python Docstrings (With Examples)
Note: We can also use triple """ quotations to create docstrings. ... Comments are descriptions that help programmers better understand the intent and functionality of the program. They are completely ignored by the Python interpreter.
🌐
Stack Abuse
stackabuse.com › common-docstring-formats-in-python
Common Docstring Formats in Python
August 26, 2023 - Docstrings in Python are a powerful tool for documenting your code. They're essentially comments that are written in a specific format, which allows them to be parsed by documentation generation tools. There are several common formats for writing docstrings, and they each have their own strengths and weaknesses. The most commonly used formats are reStructuredText (reST), Google, NumPy/SciPy, and Epytext. Note: It's important to keep in mind that the best docstring format for you depends on your specific use case.
🌐
Tutorialspoint
tutorialspoint.com › python › python_docstrings.htm
Python - Docstrings
Home Whiteboard Practice Code Graphing Calculator Online Compilers Articles Tools ... Python TechnologiesDatabasesComputer ProgrammingWeb DevelopmentJava TechnologiesComputer ScienceMobile DevelopmentBig Data & AnalyticsMicrosoft TechnologiesDevOpsLatest TechnologiesMachine LearningDigital MarketingSoftware QualityManagement Tutorials View All Categories ... In Python, docstrings are a way of documenting modules, classes, functions, and methods.
🌐
Towards Data Science
towardsdatascience.com › home › latest › five tips to elevate the readability of your python code
Five Tips to Elevate the Readability of your Python Code | Towards Data Science
March 5, 2025 - Image: Unsplash tl;dr Utilise auto-formatters such as black and isort Use code checkers such as flake8 and pylint Add type hints to remove ambiguity in your function arguments Automate code quality checking with pre-commit Write good documentation ...
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.7 documentation
then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i by assignment. __doc__ is also a valid attribute, returning the docstring belonging to the class: "A simple example class".
🌐
Medium
medium.com › @paolo.salvatori › why-your-python-code-needs-docstrings-more-than-ever-cd3a15eeef21
Why Your Python Code Needs Docstrings More Than Ever | by Paolo Salvatori | Medium
February 18, 2026 - More than just comments, docstrings are built-in documentation that explain the purpose, usage, and behavior of your code. Adopting a consistent practice of writing docstrings for all public classes, methods, and functions is a best practice, ...
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Unlike regular comments, which explain individual lines of code, docstrings provide high-level descriptions of what a function, class, or module does. Well-written docstrings improve code readability, maintainability, and collaboration, making them a best practice for documenting your Python ...
🌐
Wordpress
pythontrainingblog.wordpress.com › 2024 › 05 › 23 › best-practices-for-python-code-documentation
Best Practices for Python Code Documentation
May 23, 2024 - Here are some best practices for effective Python code documentation@ www.nearlearn.com: Describe our code. ... Create docstrings for all public classes, methods, functions, and modules.