Python Reference ยท Built-in Functions
Python help() function
Reference for Python help(): read documentation for functions, classes and modules from the interactive prompt, how docstrings feed it, and what it prints.
The built-in help() function shows documentation for an object. Pass it a function, class, module or even a string naming one, and Python prints the signature and docstring (and, for classes and modules, a list of their members) using the pydoc module.
It is mostly a tool for the interactive prompt: type help(str.split) and read the answer without leaving your terminal. Everything it shows comes from docstrings and signatures, so the documentation you write for your own functions appears there too.
def area(width, height): """Return the area of a rectangle.""" return width * height help(area)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
help() help(request)
| Name | Description |
|---|---|
request |
Optional. An object (function, class, module, method) or a string such as "str.split" or "os.path". With a string, help() looks the name up for you, importing the module if needed. |
Return value: None. The documentation is written to standard output.
Raises: with a string that cannot be found, it prints a "No Python documentation found" message instead of raising.
Usage notes
- No argument starts interactive help.
help()alone enters a prompt (help>) where you type topic names andquitto leave. That needs a real terminal, so avoid it in scripts and in the browser runner. - It only prints. To get the text as a value, read
obj.__doc__or useinspect.getdoc(obj), which also cleans up the indentation. - Docstrings drive it. A function without a docstring still shows its name and signature, but nothing else. Write the docstring as the first statement of the function.
- Large output. Help for a class or module can be long. In a terminal it is shown through a pager, which you leave with
q. - Built-in signatures vary. Output for built-in functions can differ a little between Python versions, so do not parse it. Use
inspect.signature()when a program needs the signature. - Not a debugger. It documents objects; to list what an object has, use
dir(), and for its attributes as a dict seevars(). - Defined by
site.helpis added to the builtins by thesitemodule, so it may be missing when Python starts with-S.
Related
dir(),vars(),type(),callable()breakpoint()- Lessons: Functions, Running Python