Documentation Strings

If the first statement of a module, class, or function definition is a string, that string becomes a documentation string for the associated object, as in the following example:

def fact(n):
    "This function computes a factorial"
    if (n <= 1): return 1
    else: return n*fact(n-1)

Code-browsing and documentation-generation tools sometimes use documentation strings. The strings are accessible in the __doc__ attribute of an object, as shown here:

>>> print fact.__doc__
This function computes a factorial
>>>

The indentation of the documentation string must be consistent with all the other statements in a definition.

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.