Python help Function
Last modified April 11, 2025
This comprehensive guide explores Python's help
function, which
provides interactive documentation for Python objects. We'll cover basic
usage, modules, classes, functions, and custom help documentation.
Basic Definitions
The help
function is Python's built-in interactive help system.
When called with no arguments, it starts the interactive help utility.
With arguments, it displays documentation about the specified object. It works with modules, functions, classes, methods, keywords, and other Python objects.
Basic Interactive Help
Here's how to use help
in interactive mode to explore Python's
documentation system.
# Start interactive help help() # Then try these commands at the help> prompt: # help> list # help> str.split # help> keywords # help> modules # help> quit
This shows how to enter Python's interactive help system. Once inside, you can explore documentation for various objects, keywords, and modules.
Type quit
to exit the interactive help system and return to
the Python interpreter.
Getting Help on Built-in Functions
You can get documentation for any built-in function by passing it to
help
. This example shows how to get help for print
.
# Get help for the print function help(print) # Output will show: # print(...) # print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) # Prints the values to a stream, or to sys.stdout by default...
This displays the full documentation for the print
function,
including its parameters and default values. The same approach works for
all built-in functions.
The output shows the function signature, parameter descriptions, and usage examples when available.
Exploring Module Documentation
help
can display documentation for entire modules. This example
shows how to get help for the math
module.
import math # Get help for the entire math module help(math) # Output will show: # NAME # math - This module provides access to the mathematical functions... # # DESCRIPTION # This module provides access to the mathematical functions... # # FUNCTIONS # acos(x, /) # Return the arc cosine (measured in radians) of x...
This displays comprehensive documentation for the math
module,
including all available functions, constants, and their descriptions.
You can scroll through the output to explore all available mathematical operations in the module.
Getting Class Documentation
help
provides detailed information about classes, including
methods, attributes, and inheritance. This example examines the list
class.
# Get help for the list class help(list) # Output will show: # class list(object) # | list() -> new empty list # | list(iterable) -> new list initialized from iterable's items # | # | Methods defined here: # | # | append(self, object, /) # | Append object to the end of the list...
This displays the complete class documentation, including constructor signatures, all available methods, and their descriptions.
The output helps understand how to use the class and what operations it supports.
Custom Help Documentation
You can provide help documentation for your own functions and classes using docstrings. This example demonstrates custom help.
def calculate_area(radius): """Calculate the area of a circle. Args: radius (float): The radius of the circle in meters Returns: float: The area in square meters """ return 3.14159 * radius ** 2 # Now get help for our function help(calculate_area) # Output will show: # calculate_area(radius) # Calculate the area of a circle. # # Args: # radius (float): The radius of the circle in meters # # Returns: # float: The area in square meters
This shows how docstrings become part of the help system. The function's
docstring appears when help
is called on it.
Well-written docstrings make your code self-documenting and more usable through the help system.
Best Practices
- Use docstrings: Always document your functions and classes with docstrings
- Be descriptive: Include parameter types, return values, and examples
- Explore interactively: Use help() in the REPL to discover functionality
- Check modules: Use help on imported modules to learn their API
- Follow conventions: Use standard docstring formats like Google or NumPy style
Source References
Author
List all Python tutorials.