How to write help/description text for Python functions

user1984653 picture user1984653 · Feb 21, 2014 · Viewed 43.3k times · Source

I recently started programming using Python. I have to write many functions and was wondering how I can incorporate a help or description text such that it appears in the object inspector of spyder when I call the function. In MatLab, this worked by putting commented text at the beginning of the function file. Is there a similar method in Python (using Spyder)?

Answer

Burhan Khalid picture Burhan Khalid · Feb 21, 2014

By default, the first string in the body of a method is used as its docstring (or documentation string). Python will use this when help() is called for that method.

def foo(bar):
    """
    Takes bar and does some things to it.
    """
    return bar

help(foo)
foo(bar)
    Takes bar and does
    some things to it

You can read more about how this works by reading PEP-258, and this question goes into some more details.