I've always followed the guideline that the code tells you what it does, the comments are added to explain why it does something.

If you can't read the code, you have no business looking at it, so having (in the extreme):

index += 1   # move to next item

is a total waste of time. So is a comment on a function called calculate_inverse(matrix) which states that it calculates the inverse of the matrix.

Whereas something like:

# Use Pythagoras theorem to find hypotenuse length.
hypo = sqrt (side1 * side1 + side2 * side2)

might be more suitable since it adds the information on where the equation came from, in case you need to investigate it further.

Comments should really be reserved for added information, such as the algorithm you use for calculating the inverse. In this case, since your algorithm is simply handing off the work to scipy, it's totally unnecessary.

If you must have a docstring here for auto-generated documentation, I certainly wouldn't be going beyond the one-liner variant for this very simple case:

"""Return the inverse of a matrix"""
Answer from paxdiablo on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › docstrings - should main() method have an examples section?
r/learnpython on Reddit: Docstrings - should main() method have an examples section?
December 7, 2020 -

Hello, all!

I've been working on my docstrings skills and I've ran into the problem of how to write a docstring for my main method. I couldn't find much with my google fu, so I'm thinking that this might fall to an opinion question.

So, if my driver script (named prog.py) looks like

#! python

""" prog.py """

def main():
    """
    Main method.

    Main method ran when executing the prog.py file from console.
    This method is not designed to be called like a method from a module.

    Parameters
    -------------
    None

    Returns
    ---------
    None

    Raises
    -------
    None

    Examples
    -----------
    ????????????
    """

    pass

if __name__ == '__main__':

    main()

If I never intend to allow my main method to be imported and ran like a module method, do you have any suggestions on how to write the examples section of the docstring?

Do you feel that a docstring is not needed for the main method()?

Thank you so much in advance for your opinions and advice!

EDIT: Formatting and engrish.

🌐
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 Function A (funcA): This function accepts no inputs, returns no outputs and does no work
Discussions

docstring - Should you always document functions, even if redundant (specifically python)? - Stack Overflow
I try to use function names that are active and descriptive, which I then document with active and descriptive text (!). This generates redundant-looking code. Simplified (but not so unrealistic) example in python, following numpy docstring style: More on stackoverflow.com
🌐 stackoverflow.com
Advice on writing some docstrings
Now assuming I need to understand on an intuitive, conceptual level what all these functions really do, and I assume there’s no documentation for them because it’s generated from the docstrings, should I open a Python shell, import this module and just go through each method, executing ... More on reddit.com
🌐 r/learnpython
7
3
May 6, 2022
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
Do short functions need docstrings?
If you write a function that is 1 or 2 lines long, does it still need a docstring? If so, what needs to be in the docstring? probably. depending on the tools used the docstring can become important. at most.. it doesn't hurt.. and takes like few seconds to do it... so .. if not the functions/methods .. at least the class. almost all the time, when i go looking into packages and find what i'm looking for .. they have wonderful descriptions of the classes .. it is incredibly helpful to try and understand what the creator of the code wanted to do . i find them particularly useful. More on reddit.com
🌐 r/learnpython
3
1
June 9, 2023
🌐
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.
🌐
LabEx
labex.io › tutorials › python-how-to-document-a-python-function-using-docstrings-417961
How to document a Python function using docstrings | LabEx
Docstrings serve as the first line of documentation for your code, offering a concise explanation of what a particular piece of code does. They are particularly useful for documenting Python functions, as they allow you to describe the purpose, ...
🌐
Board Infinity
boardinfinity.com › blog › python-docstring
Python Docstring | Board Infinity
August 13, 2025 - In Python, comments are used to describe the code so that the user who reads the code understands the source code. It describes the logic or a block of code. It can be written by using a #, on the other hand, Python docstrings give us a convenient path to associate documentation with python modules, methods, functions and classes.
🌐
Dataquest
dataquest.io › blog › documenting-in-python-with-docstrings
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - The Python docstring of this function is enclosed between three double quotes from both sides. As you can see, this string explains what this function does and indicates how we can change its functionality — and what happens if it doesn't ...
Find elsewhere
🌐
Llego
llego.dev › home › blog › write python docstrings: guide to documenting functions
Write Python Docstrings: Guide to Documenting Functions - llego.dev
May 31, 2023 - There are some standard conventions and best practices to follow when writing effective Python docstrings: Be concise yet descriptive - Stick to the essence but provide sufficient details on usage and purpose. Verbose docstrings are ignored. Use triple double quotes - Docstrings are surrounded by triple quotes rather than single quotes. ... Follow a standard format- PEP 257 convention provides guidelines on formatting docstrings with uniformity. Document all public interfaces - Provide a docstring for any function, class, or module that is part of the public API.
🌐
Readthedocs
sphinx-rtd-tutorial.readthedocs.io › en › latest › docstrings.html
Writing docstrings — Sphinx-RTD-Tutorial documentation
If you are using VS code, the Python Docstring extension can be used to auto-generate a docstring snippet once a function/class has been written.
🌐
Sphinx
sphinx-doc.org › en › master › usage › extensions › example_google.html
Example Google Style Python Docstrings — Sphinx documentation
""" def __init__(self, msg, code): self.msg = msg self.code = code class ExampleClass: """The summary line for a class docstring should fit on one line. If the class has public attributes, they may be documented here in an ``Attributes`` section and follow the same formatting as a function's ``Args`` section.
Top answer
1 of 4
6

I've always followed the guideline that the code tells you what it does, the comments are added to explain why it does something.

If you can't read the code, you have no business looking at it, so having (in the extreme):

index += 1   # move to next item

is a total waste of time. So is a comment on a function called calculate_inverse(matrix) which states that it calculates the inverse of the matrix.

Whereas something like:

# Use Pythagoras theorem to find hypotenuse length.
hypo = sqrt (side1 * side1 + side2 * side2)

might be more suitable since it adds the information on where the equation came from, in case you need to investigate it further.

Comments should really be reserved for added information, such as the algorithm you use for calculating the inverse. In this case, since your algorithm is simply handing off the work to scipy, it's totally unnecessary.

If you must have a docstring here for auto-generated documentation, I certainly wouldn't be going beyond the one-liner variant for this very simple case:

"""Return the inverse of a matrix"""
2 of 4
4

"Always"? Definitively not. Comment as little as possible. Comments lie. They always lie, and if they don't, then they will be lying tomorrow. The same applies to many docs.

The only times (imo) that you should be writing comments/documentation for your code is when you are shipping a library to clients/customers or if you're in an open source project. In these cases you should also have a rigorous standard so there is never any ambiguity what should and should not be documented, and how.

In these cases you also need to have an established workflow regarding who is responsible for updating the docs, since they will get out of sync with the code all the time.

So in summary, never ever comment/document if you can help it. If you have to (because of shipping libs/doing open source), do it Properly(tm).

🌐
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 - It is used to describe the functionality of a module or a function including the parameters and return, should they be present. In my many encounters with teaching material for Python, I have never come across any content ...
🌐
Reddit
reddit.com › r/learnpython › advice on writing some docstrings
r/learnpython on Reddit: Advice on writing some docstrings
May 6, 2022 - What it means is that self._instance is believed to have a type of list with elements being of type Service (it's actually wrong, because the code doesn't need it to be a list, it just needs to be something that has methods copy() and append(), but this kind of mistake is very typical of Python as of late. ... When writing docstrings you should adhere to some style.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Example 2: This function shows how to use triple double quotes for docstrings. ... def my_func(): """This is a docstring using triple double quotes.""" return None print(my_func.__doc__) ... This is a docstring using triple double quotes. Google style docstrings follow a specific format and are inspired by Google's documentation style guide. They provide a structured way to document Python code, including parameters, return values and descriptions.
🌐
Medium
medium.com › @amarjithkarippath › python-docstrings-dac4855d1d7a
Python Docstrings. Python documentation strings (or… | by Amarjith Karippath | Medium
August 1, 2019 - Summary line. Extended description of function. Parameters: arg1 (int): Description of arg1 Returns: int: Description of return value ... The entire docstring is indented the same as the quotes at its first line. Docstring processing tools will strip a uniform amount of indentation from the second and further lines of the docstring, equal to the minimum indentation of all non-blank lines after the first line.
🌐
JetBrains
jetbrains.com › help › pycharm › using-docstrings-to-specify-types.html
Specify types with docstrings | PyCharm Documentation
September 1, 2025 - Place the caret at the function name, and press Alt+Enter. In the list of intention actions that opens, choose Specify return type in docstring.
🌐
Kaggle
kaggle.com › code › colinmorris › functions-and-getting-help
Functions and Getting Help
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
LabEx
labex.io › questions › what-is-a-docstring-in-python-53
What is a docstring in Python | LabEx
July 25, 2024 - ## What is a Docstring in Python? In Python, a docstring (short for "documentation string") is a string literal that appears as the first statement in a Python module, function, class, or method definition.
🌐
GitHub
gist.github.com › redlotus › 3bc387c2591e3e908c9b63b97b11d24e
Google Style Python Docstrings · GitHub
Google Style Python Docstrings. GitHub Gist: instantly share code, notes, and snippets.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python Docstring Formats (Styles) and Examples | note.nkmk.me
August 26, 2023 - Built-in Functions - help() — Python 3.11.5 documentation · help(my_func) # Help on function my_func in module __main__: # # my_func() # docstring-test # line1 # line2 # line3 #