Interactively, you can display it with:

help(my_func)

Or from code you can retrieve it with (surround it with print(.) to get a formatted output):

my_func.__doc__
Answer from unwind on Stack Overflow
🌐
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 - Docstrings (Documentation Strings) are special strings used to document Python code. They provide a description of what a module, class, function or method does.
Discussions

Advice on writing some docstrings
You can configure sphinx if that's what you are using for documentation to document __init__ method separately, however, the default is to use the documentation for the class to describe that. I don't like the default and usually configure sphinx not to do that, but you need not do the same. If you are going with defaults, then the class documentation may include :ivar : for class fields. You can also include :param <__init__ param>: in that documentation to document parameters supplied to __init__. The other thing: you misinterpreted type annotation to have some procedural semantics. 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. More on reddit.com
🌐 r/learnpython
7
3
May 6, 2022
python - Getting the docstring from a function - Stack Overflow
I have the following function: def my_func(): """My docstring is both funny and informative""" pass How do I get access to the docstring? More on stackoverflow.com
🌐 stackoverflow.com
coding style - What are the most common Python docstring formats? - Stack Overflow
I find the "signature in docstrings"-style awfully redundant and verbose. For Python 3+, Function annotations are a much cleaner way to do this. Even worse if it uses pseudo-strong-types: Python is way better with duck typing. More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Real Python
realpython.com › documenting-python-code
Documenting Python Code: A Complete Guide – Real Python
December 21, 2023 - Documenting Your Python Code Base Using Docstrings: A deep dive into docstrings for classes, class methods, functions, modules, packages, and scripts, as well as what should be found within each one
🌐
Programiz
programiz.com › python-programming › docstrings
Python Docstrings (With Examples)
Python docstrings are the string literals that appear right after the definition of a function, method, class, or module.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python function docstrings
Python Function Docstrings
March 26, 2025 - The PEP 257 provides the docstring conventions. When the first line in the function body is a string, Python will interpret it as a docstring.
🌐
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.
🌐
Mimo
mimo.org › glossary › python › docstrings
Python Docstrings: Syntax, Usage, and Examples
Use Python docstrings to document functions, classes, and modules. Access them with help(), generate documentation, and improve code readability.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › advice on writing some docstrings
r/learnpython on Reddit: Advice on writing some docstrings
May 6, 2022 -

I need to write docstrings for every method and property in this file:

https://github.com/golemfactory/yapapi/blob/master/yapapi/services/service_runner.py

A couple questions.

Should you write a docstring for an init method? I suppose it depends? This one seems self explanatory. Should I just state what the code does? “ServiceRunner class is initialized with four parameters: job, instance, instance_tasks, and stopped.”

I could explain what each of those do. I actually have some questions about them. “Job” is clearly passed the string “job”, so I don’t understand the later call “job.id” - the string returns an ID?

As for: self._instances: List[Service] = [] - how can you pass an entire statement as an attribute? They convert “Service” to a list but then assign it as an empty list… will the result be the list of services or the empty list?

Just that for now. Please let me know if you understand this a bit better than I do.

Thanks very much

Top answer
1 of 4
4
You can configure sphinx if that's what you are using for documentation to document __init__ method separately, however, the default is to use the documentation for the class to describe that. I don't like the default and usually configure sphinx not to do that, but you need not do the same. If you are going with defaults, then the class documentation may include :ivar : for class fields. You can also include :param <__init__ param>: in that documentation to document parameters supplied to __init__. The other thing: you misinterpreted type annotation to have some procedural semantics. 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.
2 of 4
3
When writing docstrings you should adhere to some style. First see if there exists any existing style guides for the project you are working on. If not then pick a style and stick to it. I prefer Google's docstring style https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings Also note that plugins helps a bunch. Whenever I need a docstring I just hit ,cn and it inserts a boilerplate docstring in the current function. These also exists for VScode, pycharm, etc. Just google a bit for your editor. For examples see for instance https://github.com/psf/requests/blob/main/requests/adapters.py
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Python documentation string, commonly known as docstring, is a string literal, and it is used in the class, module, function, or method definition. Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function.
🌐
Python-sprints
python-sprints.github.io › pandas › guide › pandas_docstring.html
pandas docstring guide — Python documentation
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.
🌐
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 ...
🌐
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, ...
🌐
Pandas
pandas.pydata.org › docs › development › contributing_docstring.html
pandas docstring guide — pandas 3.0.2 documentation
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.
🌐
Wikipedia
en.wikipedia.org › wiki › Docstring
Docstring - Wikipedia
December 19, 2025 - When they are kept, docstrings may be viewed and changed using the DOCUMENTATION function. For instance: (defun foo () "hi there" nil) (documentation #'foo 'function) => "hi there" In Python, a docstring is a string literal that follows a module, class or function definition.
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
June 19, 2025 - In this format, Args lists parameters and their descriptions, Returns describes the return value and its type, and Raises (when included) shows exceptions that might be raised by the function. Google-style docstrings shine when you need a detailed, consistent structure—especially if you’re collaborating on large projects or using documentation generators like Sphinx. The NumPy style of docstrings is favored in scientific and data-oriented Python projects.
🌐
Readthedocs
sphinxcontrib-napoleon.readthedocs.io › en › latest › example_google.html
Example Google Style Python Docstrings — napoleon 0.7 documentation
If attribute, parameter, and return ... 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_pep4...
Top answer
1 of 6
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 6
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
🌐
Zero To Mastery
zerotomastery.io › blog › python-docstring
Beginner's Guide to Python Docstrings (With Code Examples) | Zero To Mastery
September 27, 2024 - Think of them as mini-explanations that stick with your functions, classes, or modules. They live inside your code, but they’re also accessible through Python's help() function, making them perfect for creating formal documentation.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python Docstring Formats (Styles) and Examples | note.nkmk.me
August 26, 2023 - In Python, strings written at the beginning of definitions such as functions and classes are treated as docstrings (documentation strings). IDEs or editors may offer keyboard shortcuts to display docs ...