Register Login

Python’s help() Function

Updated Oct 28, 2021

We all need help at some point in time when we get stuck. Every time a teacher or a senior will not remain present to help us out. While writing a Python code, we might need to know the meaning of certain Python keywords, classes, modules, functions, etc. Luckily, Python provides a solution to such desperate situations also. In this article, you will learn about the help() function of Python.

What is Python’s help() function?

The help() function in Python generates documentation for a certain Python token such as keywords, classes, modules, functions, etc. We can use it in the Python's console or write it on the scripting section also.

The syntax of using it is:

help(object)

Here the object is the parameter that the help() function takes for providing the documentation about that particular Python keyword, class, module, function.

Different ways of using help():

There are two different ways of using the help(). These are:

Python programmers can directly use the help in the console. For this the programmer has to call the help() function directly right after the Python prompt.

>>> help(['1', '2', '3'])

Or,

>>> help(print)

Python programmers can also use the help() function within the Script mode. But in this situation, the programmer has to use the help() function within the print() function. Otherwise, the documentation will not get displayed.

print(help(['1', '2', '3']))

or,

a= help(['1', '2', '3'])
print(a)

Output:

Passing string argument to help() function:

When a programmer passes a string within the help() as a parameter, the function converts the string into a valid Python token name such as a keyword, module, function, method, class, or documentation topic. It will then display the corresponding help documentation associated with that string name. A single quote or double quote is a valid way of mentioning the string within the help() function.

For example:

>>> help('for')
>>> help("def")

Output:

help() Works with Library:

There are situations where the help() function demands importing the library for invoking the documentation for any specific method or function residing within that library or module. Let us take a situation where:

print(help(log))

or,

print(help(math.log))

In such situation, programmers need to use the import statement and include the library name within the program. Then using the help() function will work fine as the library contains the documentation that gets invoked by the help() function.

Example:

import math
print(help(math.log))

Output:

The help() function with no argument:

We can use the help function without passing any argument to it. If you run this function with no argument within it, it will automatically start the interactive Python's help utility on the interpreter console.

>>> help()

For script mode, we can use the print(help()). It also opens the help utility in the console section.

Example:

print(help())


Output:

 

Now, from this section, you can type any desired keywords, classes, modules, functions, etc. and it will bring the documentation associated with that token.

 

Creating custom help() documentations for user-defined functions:

It is also possible for programmers to prepare documentation for custom-built or user-defined functions. Programmers need to take the help of the docstring to do the same. The docstrings are declared using ”’ a collection of triple single quotes”’ or “”” a collection of triple-double quotes””” right under the function, method, or class declaration.

When we provide the docstring under the function body, the Python interpreter accepts it as documentation expressing what the function does.

Program:

def addi(a, b, c):
    """
    This function adds three given integers, g, h, and i
    :1st param: integer
    :2nd param : integer
    :3rd param : integer
    :returns: integer
    """
    return a + b + c

print(help(addi))

Output:

 

Conclusion:

Since you have gone through the comprehension about Python help, now you won't stuck and can easily seek help of this function. It is always recommend to use the console mode for seeking help using the help(). Help using the console mode is a straightforward way of getting and reading the documentation.


×