The class documentation should include the public components of the object. The __init__
parameters may or may not be public, so whether they are included in the class docstring or not depends on the object design.
The full paragraph in PEP 257 states:
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.
And the google style guide states:
Classes should have a docstring below the class definition describing
the class. If your class has public attributes, they should be
documented here in an Attributes section and follow the same
formatting as a function’s Args section.
So the documentation determination hinges on whether the parameters for __init__
are public. If the intention of the object is for users to construct their own instances, the __init__
parameters should be documented in the class docstring. However, if an object is constructed internally then returned to the user, the class documentation should only refer to the public aspects of the object.
So the following example from google suggest that the likes_spam
parameter from __init__
is public:
class SampleClass(object):
"""Summary of class here.
Longer class information....
Longer class information....
Attributes:
likes_spam: A boolean indicating if we like SPAM or not.
eggs: An integer count of the eggs we have laid.
"""
def __init__(self, likes_spam=False):
"""Inits SampleClass with blah."""
self.likes_spam = likes_spam
self.eggs = 0
def public_method(self):
"""Performs operation blah."""
However, below the public likes_spam
attribute is determined during __init__
based on two internal parameters spam_count
and like_threshold
. So, likes_spam
is documented in the class docstring, while spam_count
and like_threshold
are documented in the __init__
docstring.
class SampleClass(object):
"""Summary of class here.
Longer class information....
Longer class information....
Attributes:
likes_spam: A boolean indicating if we like SPAM or not.
eggs: An integer count of the eggs we have laid.
"""
def __init__(self, spam_count, like_threshold=1):
"""Inits SampleClass.
Args:
spam_count: The amount of SPAM consumed.
like_threshold: The threshold consumed that indicates
whether we like SPAM.
"""
self.likes_spam = spam_count > like_threshold
self.eggs = 0
def public_method(self):
"""Performs operation blah."""