Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put docstrings on special methods?

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?

like image 428
Lauritz V. Thaulow Avatar asked Apr 02 '11 19:04

Lauritz V. Thaulow


People also ask

What are docstrings how are they useful?

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.

Where does Python put 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.

Which is the correct location to place a docstring for a function?

A docstring is a string literal placed in the first line of a function body, as shown below.

Which of the following statements about docstrings are true?

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.


1 Answers

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.

like image 149
Sven Marnach Avatar answered Oct 19 '22 22:10

Sven Marnach