Python help() function
Practical guide to the Python help() function: syntax, interactive console, examples, custom docstrings, and best practices.
Drake Nguyen
Founder · System Architect
Python help() function — Quick Guide
The Python help() function is a built-in introspection utility that displays documentation for modules, classes, functions, and objects. It’s especially useful inside the Python REPL (interactive console) to quickly read docstrings, signatures, and descriptions without leaving the interpreter.
Syntax
Basic usage follows the form:
help([object])
When called without an argument, help() launches the interactive help utility. When given an object, name, or dotted path it prints the relevant documentation.
Interactive help console
Start the Python interpreter and type help() to open the built-in help system. You can then enter names or keywords such as:
help> True
help> collections
help> builtins
help> modules
help> keywords
help> topics
help> LOOPING
To exit the help console, type quit or press Ctrl+D (on many systems).
Direct queries (no interactive console)
You can request documentation directly from the interpreter by passing an object or name to help():
>> help('collections')
>>> help(print)
>>> help(globals)
Examples
Example output for builtins like globals (format varies by Python version):
>> help('builtins.globals')
Help on built-in function globals in builtins:
builtins.globals = globals()
Return the dictionary containing the current scope's global variables.
Defining help() output for custom classes and functions
help() shows a function or class docstring when available. Add docstrings using triple quotes at the start of the object body. Example module python_help_examples.py:
def add(x, y):
"""
Add two integers and return the result.
Parameters
----------
x : int
First addend.
y : int
Second addend.
Returns
-------
int
Sum of x and y.
"""
return x + y
class Employee:
"""
Simple Employee record used for demonstration.
"""
def __init__(self, id, name):
"""
Initialize an Employee.
Parameters
----------
id : int
Employee identifier (positive integer).
name : str
Employee name.
"""
self.id = id
self.name = name
Load the definitions in an interactive session (for example using exec(open(...).read())) and then use help:
>> exec(open('python_help_examples.py').read())
>>> help('python_help_examples')
>>> help('python_help_examples.add')
>>> help('python_help_examples.Employee.__init__')
help() vs dir() vs type()
- help() — Shows documentation, signatures, and docstrings (useful for reading intended usage).
- dir() — Lists an object’s attributes and names; good for discovery but not explanatory text.
- type() — Returns the actual type of an object; useful to verify what you’re inspecting.
Tips and best practices
- Always write clear docstrings for public modules, classes, and functions so
help()surfaces meaningful guidance. Consider using a consistent style such as NumPy or Google docstring formats. - Use
pydocor theinspectmodule for programmatic access to documentation when building tools or automated docs. - In the REPL,
help()is handy for quick checks of function signatures, expected parameters, and return values. - For long modules, use
help('module_name')to view available objects and their docstrings.
Summary
The Python help() function is a built-in help utility and documentation viewer that leverages docstrings and interpreter metadata. It’s a fast, interactive way to access Python documentation and inspect custom code as long as you provide meaningful docstrings.