Python help() Function



The python help() function is a built-in help system that can be invoked on any object, class, function or module to collect more information about it.

If an argument is passed to this function, a help page on that argument is generated. In the case, when no arguments are passed an interactive help system shown on the interpreter console.

In the next few section, we will be learning more about this function.

Syntax

The syntax of the python help() function is as follows −

help(object)

Parameters

The python help() function accepts a single parameter −

  • object − This parameter specifies an object for which we need help.

Return Value

The python help() function returns the information related to the passed object.

Examples

The below examples help us understand the working of help() function −

Example 1

The following example shows the basic usage of the python help() function. In this, we are passing the built-in function "print()" as an argument to the help() function which will display the brief description of the function.

help(print)

On executing the above program, the following output is generated −

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.

Example 2

If we want to show the details of a Python built-in module, we simply need to import it and pass that module to the help() function as a parameter value.

import math
help(math)

The following is the output obtained by executing the above program −

Help on built-in module math:
NAME
    math
DESCRIPTION
    This module provides access to the mathematical functions
    defined by the C standard.

Example 3

The python help() function returns the description of a class if we pass the class name as an argument to it. It will display the method and other attributes of the passed class.

class NewClass:
   def firstExample(self):
      """
      This is an example of NewClass.
      """
      pass

help(NewClass)

The following output is obtained by executing the above program -

Help on class NewClass in module __main__:

class NewClass(builtins.object)
 |  Methods defined here:
 |  
 |  firstExample(self)
 |      This is an example of NewClass.

Example 4

With the help of Python help() function, we can also display the description of a local method as illustrated in the below example.

class NewClass:
   def firstExample(self):
      """
      This is an example of NewClass.
      """
      pass

newObj = NewClass()
help(newObj.firstExample)

The above program, on executing, displays the following output −

Help on method firstExample in module __main__:

firstExample() method of __main__.NewClass instance
    This is an example of NewClass.
python_built_in_functions.htm
Advertisements