Help() function in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
194083
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
194083
4.90
Start Learning
Topics Covered

Overview

The Python help() function invokes the built-in interactive help system that displays the documentation of modules, functions, classes, keywords, etc.

Syntax of help() in Python

The syntax for help() is

Parameters of help() in Python

The help() function in python programming takes one parameter.

object (optional)- to call the help of the given object If no argument is passed, the interactive help utility starts up on the console.

Return Values of help() in Python

The help() function in python returns a help page with detailed documentation of a particular object passed as a parameter(if any).

Example of help() in Python

Let us see the implementation of help() on the print function in python.

Code:

Output:

The above program displays the documentation of python's built-in print function.

What is help() in Python, and how does it work?

Python has an in-built system using which we can get help regarding any module, classes, functions, and keywords. This help utility can be accessed using Python's help() function in the REPL. When we call this function and pass an object to it, it returns the help page or the documentation for the object.

Python help() docstring

The docstrings (documentation strings) are the '''triple single quotes''' or """triple double quotes""" that are declared just below the class, method, or function declaration. These docstrings can be accessed using the __doc__ method or the help function.

Code:

Output:

In the above program, we created a test function having docstrings declared, and we accessed it using the __doc__ first and then using the help function.

Examples of help() in Python

Example 1: Help on User-Defined Classes

The Help function output can also be defined for user-defined functions and classes. These are nested inside triple quotes and are the first statement within a class, function, or module.

Code:

Output:

Example 2: Help on the class list in module built-in

If the object is a list, it opens the list module and its class.

Code:

Output:

Example 3: What If no help or info is present?

If we encounter a situation where no help or info is present for that particular passed object, it shows a message in the console and returns None.

Code:

Output:

Example 4: What if a string is given as an argument

If the string is passed as an argument, 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.

For example -

Output:

In the above program, although we passed 'def' as a string argument, we still got the documentation for Python's defining function.

Example 5: What if no argument is passed

The help() function can be used without an argument. If you run the function without an argument, the interactive Python's help utility will be started on the interpreter console. You have to type the following command on the Python console.

Code:

This will return the Python's help utility, on which you can type the name of the object you need to get help about.

Input:

Output:

To quit the help utility and return to the interpreter, you need to type quit and press enter.

Output:

Conclusion

  • Python has an in-built system using which we can get help regarding any module, classes, functions, and keywords.
  • When we call the help() function and pass an object to it, a help page containing the documentation for the object is returned.
  • If we run help without any argument, a help utility is opened where we can get help about objects in an interactive way.
  • When no help is present for the passed argument object, it shows a message in the console and returns None.

See Also: