Python objects have an attribute called __doc__ that provides a documentation of the object. For example, you simply call
Dog.__doc__ on your class
Dog to retrieve its documentation as a string.
You simply define the docstring using a string surrounded by triple quotes as shown in the example:
class Dog: '''Your best friend.''' def do_nothing(): pass print(Dog.__doc__) # Your best friend.
As everything is an object in Python (even functions), you can also define a docstring on functions:
def bark(): '''Wuff''' pass print(bark.__doc__) # Wuff
Note that if you don’t define the docstring, the return value is
def bark(): pass print(bark.__doc__) # None
A great advantage of having defined docstrings in your code is that you now can create your own great-looking documentation programmatically. With tools such as Sphinx, it’s super easy to create something like this for your own code projects–only using the
__doc__ values defined in the code.