diveintopython.org
Python for experienced programmers

 

1.3. Documenting functions

You can document a Python function by giving it a doc string.

Example 1.4. Defining the buildConnectionString function's doc string

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters.

    Returns string."""

Triple quotes signify a multi-line string. You can use them anywhere, but you'll see them most often used when defining a doc string.

Everything between the triple quotes is the function's doc string, which documents what the function does. It is not required, but you should always do it. I know you've heard this in every programming class you've ever taken, but Python gives you an added incentive: the doc string is available at runtime as an attribute of the function.

Note
Many Python IDEs use the doc string to provide context-sensitive documentation, so that when you type a function name, its doc string appears as a tooltip. This can be incredibly helpful, but it's only as good as the doc strings you write.