Next / Previous / Contents / TCC Help System / NM Tech homepage

8.6. Documenting function interfaces

Python has a preferred way to document the purpose and usage of your functions. If the first line of a function body is a string constant, that string constant is saved along with the function as the documentation string. This string can be retrieved by using an expression of the form f.__doc__, where f is the function name.

Here's an example of a function with a documentation string.

>>> def pythag(a, b):
...     """Returns the hypotenuse of a right triangle with sides a and b.
...     """
...     return (a*a + b*b)**0.5
... 
>>> pythag(3,4)
5.0
>>> pythag(1,1)
1.4142135623730951
>>> print pythag.__doc__
Returns the hypotenuse of a right triangle with sides a and b.
    
>>>