Python help() Method

The Python help() function invokes the interactive built-in help system. If the argument is a string, then the string is treated as the name of a module, function, class, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is displayed.

Syntax:

help(object)

Parameters:

object: (Optional) The object whose documentation needs to be printed on the console.

Return Vype:

Returns a help page.

The following displays the help on the builtin print method on the Python interactive shell.

>>> help('print')
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

Above, the specified string matches with the built-in print function, so it displays help on it.

The following displays the help page on the module.

>>> help('math')
Help on built-in module math:

NAME
math

DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.

FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.

acosh(x, /)
Return the inverse hyperbolic cosine of x.

asin(x, /)
Return the arc sine (measured in radians) of x.

asinh(x, /)
Return the inverse hyperbolic sine of x.

atan(x, /)
Return the arc tangent (measured in radians) of x.
-- More --

Above, the specified string matches with the Math module, so it displays the help of the Math functions. -- More -- means there is more information to display on this by pressing the Enter or space key.

Interactive Help

If no argument is given, the interactive help system starts on the interpreter console, where you can write any function, module names to get the help, as shown below.

>>> help()
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>

Now, you can write anything to get help on. For example, write print to get help on the print function.

help> print
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

help>

Help on Classes

The help function can also be used on built-in or user-defined classes and functions.

>>> help(int)

Help on class int in module builtins:

class int(object)
| int([x]) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
|
| If x is not a number or if base is given, then x must be a string,
| bytes, or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4
|
| Methods defined here:
|
| __abs__(self, /)
| abs(self)
--More--

The following displays the docstring of the user-defined class.

Example: Help on User-defined Class
class student: 
    def __init__(self): 
        '''The student class is initialized'''
  
    def print_student(self): 
        '''Returns the student description'''
        print('student description') 

help(student)
Output
Help on class student in module __main__:

class student(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self)
 |      The student class is initialized
 |  
 |  print_student(self)
 |      Returns the student description
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)