In this article we will explore how to use the Python help() function.
Table of Contents
- Introduction
- Access documentation using interactive help utility
- Access object documentation using help()
- Access user defined function documentation using help()
- Conclusion
Introduction
In Python we often work with new modules, functions, classes, or objects that we haven’t used before, and which have documentation that we didn’t read just yet.
Instead of browsing sites with documentation looking for a specific function or class, we can use the Python help() function to get this information faster.
Python help() function is used to display documentation for a specified module, function, class, or object.
The help() function process is defined as:
help([object]) -> display documentation
Access documentation using interactive help utility
You can call the Python help() function without any arguments and it will start an interactive prompt with the help utility, which you can use to find documentation of any Python object.
Let’s start the interactive help utility:
#Start help utility
help()
and you should see a help utility start in the terminal:
Welcome to Python 3.7's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help>
Once the help utility starts, we can use it to find documentation on Python objects.
For example, let’s try to find documentation on Python map() function by running map in the help utility:
help> map
and you should get the function documentation:
Help on class map in module builtins:
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __next__(self, /)
| Implement next(self).
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
As you can see, the documentation contains the function description, its methods, and docstrings.
Access object documentation using help()
You can access the Python object documentation in one step without using the interactive help utility.
Simply run the Python help() function with a Python object passed into it as an argument in the following format:
help([object])
Let’s try to access Python map() function documentation using this method:
#Find documentation of Python map() function
help(map)
and you should get:
Help on class map in module builtins:
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __next__(self, /)
| Implement next(self).
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
As you can see, the documentation displayed is the same as the one we found using the interactive help utility.
Access user defined function documentation using help()
Python help() function can also display information of functions defined by the user.
In the previous examples we accessed documentation of Python built-in functions, so now let’s create our own function with a short description and try to access its documentation.
First, create an empty main.py file, and then let’s create a simple function that adds two numbers and returns a sum:
#Define a function
def add(x, y):
'''
This function adds two given integer arguments
Parameters:
x : integer
y : integer
Output:
val: integer
'''
val = x + y
return val
Now that we have the function defined, in the same Python file we can call the help() function and pass the function name (add) as an argument:
#Define a function
def add(x, y):
'''
This function adds two given integer arguments
Parameters:
x : integer
y : integer
Output:
val: integer
'''
val = x + y
return val
#Find documentation of user defined function add()
help(add)
and you should get:
Help on function add in module __main__:
add(x, y)
This function adds two given integer arguments
Parameters:
x : integer
y : integer
Output:
val: integer
It displayed the documentation for the function stored in the docstring, including its description, input parameters, and return value.
Conclusion
In this article we explored how to use the Python help() function, including the interactive help utility, accessing documentation of built-in functions, as well as user defined functions.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Functions tutorials.