Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docstring in class or __init__ constructor?

There seem to be two places where you can put docstrings for a class:

  1. Right under the class definition:
class MyClass(object):
     """ Summary of MyClass

     Body

     ...
     """
  1. Right under the __init__ constructor:
...
    def __init__(self, arg1, arg2):
        """ Summary of MyClass

        Body

        ...
        """

Which is preferred? Or is it okay to have both?

like image 372
Jin Avatar asked Jan 14 '19 21:01

Jin


1 Answers

They can both exist, since they are both intended for different things.

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 (in the docstring). The class constructor should be documented in the docstring for its __init__ method. Individual methods should be documented by their own docstring.

Emphasis mine. This reference is from PEP 257 -- Docstring Conventions

like image 102
wim Avatar answered Sep 17 '22 07:09

wim