Yes just drop the methods section from the class docstring. I've never ever seen something like that used.(It is used in few places in the standard library.)  The class docstring needs to just describe the class and the docstring of individual methods then handle describing themselves.

Also the wording in the PEP to me means that the class docstring "should" list the public methods, but not describe them in any other way.(This is also how the above standard library example does it.)  But as said, I would never even do that, since the code speaks for itself and that kind of listing is bound to get out-of-date.

Final note: I personally prefer to use the Google docstring style, because to me it's the clearest and cleanest.

Answer from ruohola on Stack Overflow
Top answer
1 of 2
11

Yes just drop the methods section from the class docstring. I've never ever seen something like that used.(It is used in few places in the standard library.)  The class docstring needs to just describe the class and the docstring of individual methods then handle describing themselves.

Also the wording in the PEP to me means that the class docstring "should" list the public methods, but not describe them in any other way.(This is also how the above standard library example does it.)  But as said, I would never even do that, since the code speaks for itself and that kind of listing is bound to get out-of-date.

Final note: I personally prefer to use the Google docstring style, because to me it's the clearest and cleanest.

2 of 2
2

example:

class Animal:
    """
    A class used to represent an Animal

    ...

Attributes
----------
says_str : str
    a formatted string to print out what the animal says
name : str
    the name of the animal
sound : str
    the sound that the animal makes
num_legs : int
    the number of legs the animal has (default 4)

Methods
-------
says(sound=None)
    Prints the animals name and what sound it makes
"""

says_str = "A {name} says {sound}"

def __init__(self, name, sound, num_legs=4):
    """
    Parameters
    ----------
    name : str
        The name of the animal
    sound : str
        The sound the animal makes
    num_legs : int, optional
        The number of legs the animal (default is 4)
    """

    self.name = name
    self.sound = sound
    self.num_legs = num_legs

def says(self, sound=None):
    """Prints what the animals name is and what sound it makes.

    If the argument `sound` isn't passed in, the default Animal
    sound is used.

    Parameters
    ----------
    sound : str, optional
        The sound the animal makes (default is None)

    Raises
    ------
    NotImplementedError
        If no sound is set for the animal or passed in as a
        parameter.
    """

    if self.sound is None and sound is None:
        raise NotImplementedError("Silent Animals are not supported!")

    out_sound = self.sound if sound is None else sound
    print(self.says_str.format(name=self.name, sound=out_sound))

Yep, listing methods in the class docstring, then each method again documented, according to this standard. I reccomend using sphinx, though: https://www.sphinx-doc.org/en/master/contents.html

🌐
Python
peps.python.org › pep-0257
PEP 257 – Docstring Conventions | peps.python.org
The docstring for a class should summarize its behavior and list the public methods and instance variables. If the class is intended to be subclassed, and has an additional interface for subclasses, this interface should be listed separately ...
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 - How should docstrings be used in modules with one class? - Software Engineering Stack Exchange
When authoring OOP code, sometimes you have a file that only contains one class, and nothing else. PEP8 says that all modules and all classes should have docstrings outlining what they do. But in ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
October 22, 2018
Should I use docstrings for __init__ and __repr__?
While I'll admit there's probably not a heck of a lot that the __repr__ method's docstring can add to the reader's understanding, the general idea is that if you have implemented it -- and not just used the default from object -- then there must be some custom behavior, which should or could be explained. Being able to just run help(YourClass) and have a straightforward summary of all its non-default behavior can be invaluable to someone approaching your code as a black box. More on reddit.com
🌐 r/learnpython
6
2
February 11, 2018
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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-docstrings
Python Docstrings - GeeksforGeeks
September 19, 2025 - Docstrings (""" """): Special strings placed below definitions to document modules, classes or functions. Unlike comments, they can be accessed using __doc__ or help(). Note: Docstrings are actually strings too, but Python treats them specially ...
🌐
Sphinx
sphinx-doc.org › en › master › usage › extensions › example_google.html
Example Google Style Python Docstrings — Sphinx documentation
Args: msg (str): Human readable string describing the exception. code (:obj:`int`, optional): Error code. Attributes: msg (str): Human readable string describing the exception. code (int): Exception error code. """ 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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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 3
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 3
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
🌐
Noirlab
datalab.noirlab.edu › docs › manual › DevGuide › DocumentingPythonAPIswithDocstrings › DocumentingPythonAPIswithDocstrings.html
3.2. Documenting Python APIs with Docstrings — Data Lab documentation
Documenting Class Properties. ... Complete Example Module. Treat the guidelines on this page as an extension of the Data Lab Python Style Guide. Python docstrings form the __doc__ attributes attached to modules, classes, methods and functions.
🌐
Real Python
realpython.com › how-to-write-docstrings-in-python
How to Write Docstrings in Python – Real Python
August 25, 2025 - Python comments and docstrings ... notes for other developers. Docstrings describe modules, classes, and functions so users and tools can access that information at runtime....
🌐
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.
🌐
Lftechnology
coding-guidelines.lftechnology.com › docstrings
Convention for docstrings | Leapfrog Coding Guidelines
Docstrings placed arbitrarily may simply be construed as a comment · To illustrate this try the following in the python console · class Test: """This is a class docstring""" def example_method(): """This is a method docstring """ pass def example_method_2(): # This is a comment pass
🌐
Mimo
mimo.org › glossary › python › docstrings
Python Docstrings: Syntax, Usage, and Examples
Docstrings should clearly describe what the function, class, or module does. Docstrings are documentation strings embedded within code. They help programmers understand the purpose and functionality of different code components without digging into every step of the algorithm behind them.
🌐
Lsst
developer.lsst.io › v › DM-5063 › docs › py_docs.html
Documenting Python Code — LSST DM Developer Guide latest documentation
Python docstrings are special strings that form the __doc__ attributes attached to modules, classes, methods and functions.
🌐
Lsst
developer.lsst.io › v › DM-7919 › docs › py_docs.html
Documenting Python APIs — LSST DM Developer Guide latest documentation
Python docstrings are special strings that form the __doc__ attributes attached to modules, classes, methods and functions.
🌐
PythonForBeginners.com
pythonforbeginners.com › home › python docstrings
Python Docstrings - PythonForBeginners.com
August 28, 2020 - The following Python file shows the declaration of docstrings within a python source file: """ Assuming this is file mymodule.py, then this string, being the first statement in the file, will become the "mymodule" module's docstring when the file is imported. """ class MyClass(object): """The class's docstring""" def my_method(self): """The method's docstring""" def my_function(): """The function's docstring"""
🌐
Python.org
discuss.python.org › ideas
Revisiting attribute docstrings - Page 2 - Ideas - Discussions on Python.org
October 22, 2023 - PEP 224 (Attribute Docstrings) proposed a syntax for class attribute docstrings: class A: b = 42 """Some documentation.""" c = None This was rejected because of ambiguity for readers about which attribute…
🌐
Python
docs.python.org › 3 › glossary.html
Glossary — Python 3.14.7 documentation
They allow you to include unescaped single and double quotes within a string and they can span multiple lines without the use of the continuation character, making them especially useful when writing docstrings. ... The type of a Python object determines what kind of object it is; every object has a type. An object’s type is accessible as its __class__ attribute or can be retrieved with type(obj).
Top answer
1 of 2
2

When authoring OOP code, it is very common to have a file that only contains one class, and nothing else.

This is common in some languages, and may be enforced, like in Java. However, it has very little to do with OOP and more to do with the fact that Java is a popular OOP language. I write/modify several classes in a single file every day (I'm a Python dev), it's a pretty common practice.

PEP8 says that all modules and all classes should have docstrings outlining what they do. But in this case, the module is simply a container for the class. If you put a description of the class, then the information is duplicated

Yes it would be duplicated if you're following a one class per file rule. You have some choices:

  1. Stop following the rule and allow multiple classes per file (especially if you're only following it because it is a perceived best practice)

  2. Just put the docstring in the class and forget the module level docstring. If it's only one class per module (and you strictly follow OOP), then everything will be in the class, so the module docstring will be fairly meaningless. It's perfectly okay to not follow PEP8 to the letter, and in general, it's better to just do what's best for your particular situation than blindly/dogmatically following a standard or best practice.

HTH.

P.S. If a linter is complaining, and this in turn is ruining your builds, there's usually options to turn off certain PEP8 requirements. For example, ignoring E501 (line length greater than 80 chars) is a common one I see that teams choose to ignore (if the pep8 module is your linter, you can do pep8 --ignore=E501, for instance)

2 of 2
2

Style guides like PEP-8 are not absolute laws that must be followed, but only guides:

However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

— PEP 8

Where a module only contains one class, a module-level docstring is not helpful and you should probably leave it out. If you use a linter that requires this superfluous docstring, disable that linter policy for the current file.

If we read PEP-8 more closely, it recommends “docstrings for all public modules”. A module that only contains one class is usually not part of the public interface, you would instead re-export the class through an __init__.py file. Nevertheless, one class per module layouts are fairly rare in Python. You will likely have non-public helper functions or other closely related classes in the same file.

🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
The built-in string class provides the ability to do complex variable substitutions and value formatting via the format() method described in PEP 3101.