Python Documentation Strings

To document what an object does, you can place a triple-quoted string directly after the object.

Example

Using the Python Shell, enter this function:

Python
def sayHello(name):
    """A simple function that says hello"""
    print(f'Hello {name}.')

Why comment your code?

Comments are an important way to provide another programmer with a description of what your functions does. More importantly, comments provide you a to way to explain why a function is doing (or needs to do) something. It also allows you to explain anything unusual that another developer may need to know.

Never assume your code makes sense. It may make sense to you, but not to another developer who doesn't have the luxury of asking you what's going on.

You can also provide multi-line help. For example:

Python
def sayHello(name):
    """
    A simple function 
    that says hello
    """

    print(f'Hello {name}.')

Notes

You can access this an object's comment programatically using:

Python
help(sayHello)

Output

Help on function sayHello in module __main__:

sayHello(name)
    A simple function that says hello

Alternative

You can also specify the documentation for a function separate from a document string embedded in the function by assigning the __doc__ variable for that function. For example:

Python
sayHello.__doc__ = 'Say hello to someone.'

An advantage to using this style of object documentation is that it doesn't clutter your object's code with the documentation — all the documentation can be placed in a separate code file or at the end of a code file.