help()
Shows the documentation of a function, class or module: its parameters and its docstring. Most useful in the interactive Python console.
help(request)help() prints the documentation for anything you give it: help(len), help(str.split), or one of your own functions. It shows how to call it (the parameters) and the explanation the author wrote.
That explanation is the docstring: a string written as the first line inside a function or class. When you write your own functions, add a docstring, and help() will show it to anyone using your code.
help() is made for the interactive console. Called with no argument it starts an interactive help system. The text it prints for built-in things is long and can change between Python versions, so the examples here use small functions of our own.
Parameters
| Parameter | What it is |
|---|---|
requestoptional | What to explain: a function, method, class, module or object. A string such as "keywords" looks up a help topic. With no argument, interactive help starts. |
Return value
Nothing (None). It prints the documentation.
Examples
1Help on your own function
def area(width, height):
"""Return the area of a rectangle."""
return width * height
help(area)Help on function area in module __main__:
area(width, height)
Return the area of a rectangle.The first line says what it is and where it lives. __main__ means the file you are running.
2Default values and a longer docstring
The signature line shows the default values, and every line of the docstring is printed with its indentation cleaned up.
def price_with_tax(price, rate=0.2):
"""Add sales tax to a price.
rate is a fraction: 0.2 means 20%.
"""
return round(price * (1 + rate), 2)
help(price_with_tax)Help on function price_with_tax in module __main__:
price_with_tax(price, rate=0.2)
Add sales tax to a price.
rate is a fraction: 0.2 means 20%.3Read just the docstring with __doc__
help() gets its text from the __doc__ attribute. You can read it directly when you only need the explanation.
def greet(name):
"""Say hello to someone by name."""
print("Hello,", name)
print(greet.__doc__)Say hello to someone by name.
4A function without a docstring
def add(a, b):
return a + b
help(add)Help on function add in module __main__: add(a, b)
Without a docstring, help() can only show the parameter names. That is a good reason to write one.
Common mistakes
Calling the function inside help()
help(area()) runs area first and asks for help on its result. Pass the function itself, without brackets: help(area).
Putting the docstring in the wrong place
The docstring must be the very first statement inside the function. A comment (# ...) or a string placed after other code is not a docstring, and help() will not show it.
def total(prices):
# Add up all prices.
return sum(prices)
print(total.__doc__)None
Questions people ask
How do I exit help in the Python console?
In the interactive help system (started by help() with no argument), type quit or press Enter on an empty line. When a long page is shown, press q.
How do I see all methods of an object?
Use dir(): dir(str) lists every name a string has. Then call help(str.upper) for the one you want.
How do I write a good docstring?
Start with one short line that says what the function does. If needed, leave an empty line and explain the parameters and what it returns.