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. Furthermore, adjacent strings appearing on different lines are not concatenated to form a single documentation ...

Get Python Essential Reference, Second 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.