In comments section of PyCharm's manual there's a nice hint from developer:

#: :type: dict of (str, C)
#: :type: list of str

It works for me pretty well. Now it makes me wonder what's the best way to document parametrized classes in Python :).

Answer from Michael Korbakov on Stack Overflow
🌐
Python
peps.python.org › pep-0257
PEP 257 – Docstring Conventions | peps.python.org
The docstring for a module should generally list the classes, exceptions and functions (and any other objects) that are exported by the module, with a one-line summary of each.
🌐
Programiz
programiz.com › python-programming › docstrings
Python Docstrings (With Examples)
| | Parameters | ---------- | additional : str, optional | More info to be displayed (default is None) | | Returns | ------- | None | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) Here, we can see that the help() function retrieves the docstrings of the Person class along with the methods associated with that class. The docstrings for Python script should document the script's functions and command-line syntax as a usable message.
🌐
DataCamp
datacamp.com › tutorial › docstrings-python
Python Docstrings Tutorial : Examples & Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp
February 14, 2025 - Find different examples & format types of docstrings for Sphinx, Numpy and Pydoc. ... Get your team access to the full DataCamp for business platform. Run and edit the code from this tutorial onlineRun code · If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.
🌐
Readthedocs
sphinxcontrib-napoleon.readthedocs.io › en › latest › example_google.html
Example Google Style Python Docstrings — napoleon 0.7 documentation
The __init__ method may be documented in either the class level docstring, or as a docstring on the __init__ method itself. Either form is acceptable, but the two should not be mixed. Choose one convention to document the __init__ method and be consistent with it. Note: Do not include the `self` parameter in the ``Args`` section. Args: param1 (str): Description of `param1`. param2 (:obj:`int`, optional): Description of `param2`. Multiple lines are supported. param3 (:obj:`list` of :obj:`str`): Description of `param3`. """ self.attr1 = param1 self.attr2 = param2 self.attr3 = param3 #: Doc comme
🌐
Real Python
realpython.com › documenting-python-code
Documenting Python Code: A Complete Guide – Real Python
December 21, 2023 - """Gets and prints the spreadsheet's ... is False) @rtype: list @returns: a list of strings representing the header columns """ Note: To learn more about docstrings and how to create them, check out How to Write Docstrings in Python....
🌐
Readthedocs
numpydoc.readthedocs.io › en › latest › format.html
Style guide — numpydoc v1.11.0rc0.dev0 Manual
When referring to a parameter anywhere within the docstring, enclose its name in single backticks. For the parameter types, be as precise as possible. Below are a few examples of parameters and their types. Parameters ---------- filename : str copy : bool dtype : data-type iterable : iterable object shape : int or tuple of int files : list of str
🌐
Pandas
pandas.pydata.org › docs › development › contributing_docstring.html
pandas docstring guide — pandas 3.0.2 documentation
On rare occasions reST styles like bold text or italics will be used in docstrings, but is it common to have inline code, which is presented between backticks. The following are considered inline code: ... Python code, a module, function, built-in, type, literal… (e.g. os, list, numpy.abs, datetime.date, True)
Find elsewhere
🌐
Python-sprints
python-sprints.github.io › pandas › guide › pandas_docstring.html
pandas docstring guide — Python documentation
In rare occasions reST styles like bold text or itallics will be used in docstrings, but is it common to have inline code, which is presented between backticks. It is considered inline code: ... Python code, a module, function, built-in, type, literal… (e.g. os, list, numpy.abs, datetime.date, True)
🌐
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.
🌐
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 #
🌐
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.
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
🌐
Lsst
developer.lsst.io › python › numpydoc.html
Documenting Python APIs with docstrings — LSST DM Developer Guide main documentation
This module demonstrates documentation written according to LSST DM's guidelines for `Documenting Python APIs with Docstrings`_. Notes ----- Docstrings have well-specified sections. This is the Notes section. Permitted sections are listed in `Numpydoc Sections in Docstrings`_.
🌐
Dataquest
dataquest.io › blog › documenting-in-python-with-docstrings
Tutorial: Documenting in Python with Docstrings
December 13, 2024 - Let's look at the different types of docstrings. First, it's an excellent idea to browse Python's PEP pages. We can find PEP 257, which summarizes Python docstrings. I strongly recommend you read it all even though you may not understand all of it.
🌐
Tutorialspoint
tutorialspoint.com › python › python_docstrings.htm
Python - Docstrings
When writing docstrings for modules, place the docstring at the top of the module, right after any import statements. The module docstring provide an overview of the module's functionality and list its primary components, such as list of functions, classes, and exceptions provided by the module. In this example, we demonstrate the use of docstrings for modules in Python −
🌐
SourceForge
epydoc.sourceforge.net › manual-docstring.html
Python Docstrings - Epydoc
Docstrings can be accessed from the interpreter and from Python programs using the "__doc__" attribute:
🌐
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
🌐
Readthedocs
sphinx-rtd-tutorial.readthedocs.io › en › latest › docstrings.html
Writing docstrings — Sphinx-RTD-Tutorial documentation
:param uuids: A list of string service UUIDs to be discovered, defaults to None :type uuids: list, optional :return: A list of the discovered :class:`bluepy.blte.Service` objects, which match the provided ``uuids`` :rtype: list On Python 3.x, this returns a dictionary view object, not a list """ self._services = [] if(uuids is not None): for uuid in uuids: try: service = self.getServiceByUUID(uuid) self.services.append(service) except BTLEException: pass else: self._services = super().getServices() return self._services def setNotificationCallback(self, callback): """Set the callback function to be executed when the device sends a notification to the client.
🌐
Idmod
docs.idmod.org › projects › doc-guidance › en › latest › docstrings.html
Google style Python docstrings — Doc guidance documentation
See Text editors: Write the source ... to include autogenerated API reference content. The parameter list in Python docstrings is formatted very similar to a definition list....