I'm trying to decide what information to put in the class docstring and what to put in the __init__
method docstring. Up until now I've been putting an overview of the class and how to work with it in the class docstring, while stuff directly related to initialization (argument details etc.) I put in the __init__
docstring.
Today I started wondering if this was the right way of doing it, so I looked at a couple of built-in modules, and I see that the __init__
method almost never has a docstring. According to PEP8, "Docstrings are not necessary for non-public methods", but isn't __init__
public?
In the same vein, what about other special methods, like __getitem__
, __getattr__
or __new__
, should they have docstrings? Or should I just mention the consequences they have in the class docstring?
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. Also, it is a common practice to generate online (html) documentation automatically from docstrings.
The docstrings for a Python package is written in the package's __init__.py file. It should contain all the available modules and sub-packages exported by the package.
A docstring is a string literal placed in the first line of a function body, as shown below.
docstring is optional in a function, class or a module docstring is the second statement that appears in a function, class or a module. docstring is a single lie string. docstring is a document” Code Answer.
Straight from PEP 257:
Public methods (including the
__init__
constructor) should also have docstrings.
[...]
The class constructor should be documented in the docstring for its
__init__
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With