84

That's it. If you want to document a function or a class, you put a string just after the definition. For instance:

def foo():
    """This function does nothing."""
    pass

But what about a module? How can I document what a file.py does?

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
Auron
  • 13,626
  • 15
  • 47
  • 54

6 Answers6

96

Add your docstring as the first statement in the module.

"""
Your module's verbose yet thorough docstring.
"""

import foo

# ...

For packages, you can add your docstring to __init__.py.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
  • The first statement, which means I can still have comment blocks before that in the module file? For example, the `#!/usr/bin/python` line and a comment block containing the copyright notice can still come before the module docstring? – Mike Finch Sep 07 '22 at 21:26
  • 1
    Comments are ignored and do not count as statements, so a shebang at the top of the file is fine. I've done it a lot, and you can verify the docstring was read by checking the module's `__doc__` property. – Brad Koch Sep 08 '22 at 20:55
55

For the packages, you can document it in __init__.py. For the modules, you can add a docstring simply in the module file.

All the information is here: http://www.python.org/dev/peps/pep-0257/

Grégoire Cachet
  • 2,547
  • 3
  • 29
  • 27
27

Here is an Example Google Style Python Docstrings on how module can be documented. Basically there is an information about a module, how to execute it and information about module level variables and list of ToDo items.

"""Example Google style docstrings.

This module demonstrates documentation as specified by the `Google
Python Style Guide`_. Docstrings may extend over multiple lines.
Sections are created with a section header and a colon followed by a
block of indented text.

Example:
    Examples can be given using either the ``Example`` or ``Examples``
    sections. Sections support any reStructuredText formatting, including
    literal blocks::

        $ python example_google.py

Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.

Attributes:
    module_level_variable1 (int): Module level variables may be documented in
        either the ``Attributes`` section of the module docstring, or in an
        inline docstring immediately following the variable.

        Either form is acceptable, but the two should not be mixed. Choose
        one convention to document module level variables and be consistent
        with it.

Todo:
    * For module TODOs
    * You have to also use ``sphinx.ext.todo`` extension

.. _Google Python Style Guide:   
http://google.github.io/styleguide/pyguide.html

"""

module_level_variable1 = 12345

def my_function():   
    pass 
... 
...
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
8

You do it the exact same way. Put a string in as the first statement in the module.

Chris Upchurch
  • 15,297
  • 6
  • 51
  • 66
4

It's easy, you just add a docstring at the top of the module.

David Locke
  • 17,926
  • 9
  • 33
  • 53
4

For PyPI Packages:

If you add doc strings like this in your __init__.py file as seen below

"""
Please refer to the documentation provided in the README.md,
which can be found at gorpyter's PyPI URL: https://pypi.org/project/gorpyter/
"""

# <IMPORT_DEPENDENCIES>

def setup():
    """Verify your Python and R dependencies."""

Then you will receive this in everyday usage of the help function.

help(<YOUR_PACKAGE>)

DESCRIPTION
    Please refer to the documentation provided in the README.md,
    which can be found at gorpyter's PyPI URL: https://pypi.org/project/gorpyter/


FUNCTIONS
    setup()
        Verify your Python and R dependencies.

Note, that my help DESCRIPTION is triggered by having that first docstring at the very top of the file.

Kermit
  • 4,922
  • 4
  • 42
  • 74