Help function in Python
Last Updated :
29 Nov, 2023
In Python, the help()
function is a built-in function that provides information about modules, classes, functions, and modules. In this article, we will learn about help function in Python.
help() function in Python Syntax
Syntax: help([object])
Parameters (Optional) : Any object for which we want some help or the information.
If the help function is passed without an argument, then the interactive help utility starts up on the console.
What is Help function in Python?
The Python help function is used to display the documentation of modules, functions, classes, keywords, etc. It provides information about modules, classes, functions, and methods. It is a useful tool for getting documentation and assistance on various aspects of Python.
Python help() function Examples
Simple help() Program
In this example, we are using help() without any object to access documentation in Python.
Output
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 ...
Help with Print Function in Python
Let us check the documentation of the print function in the Python console.
Output
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 on User Defined Class in Python
Help function output can also be defined for user-defined functions and classes. The docstring(documentation string) is used for documentation. It is nested inside triple quotes and is the first statement within a class or function or a module. Let us define a class with functions.
Python3
class Helper:
def __init__( self ):
def print_help( self ):
print ( 'helper description' )
help (Helper)
help (Helper.print_help)
|
Output
Help on class Helper in module __main__:
class Helper(builtins.object)
| Methods defined here:
|
| __init__(self)
| The helper class is initialized
|
| print_help(self)
| Returns the help description
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
Help on function print_help in module __main__:
print_help(self)
Returns the help description
If no Help or Info is Present
In this example, we are using help() if no help or info is present and access documentation in Python.
Python3
print ( help ( "GeeksforGeeks" ))
|
Output
No Python documentation found for 'GeeksforGeeks'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
None
If a string is given as an argument
In this example, we are passing a string inside the help function to access documentation in Python.
Output
No Python documentation found for 'gfg'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
Python help() function docstring
The docstrings are declared using ”’triple single quotes”’ or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.
Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function.
Python3
def my_function():
return None
print ( "Using __doc__:" )
print (my_function.__doc__)
print ( "Using help:" )
help (my_function)
|
Output
Using __doc__:
Demonstrates triple double quotes
docstrings and does nothing really.
Using help:
Help on function my_function in module __main__:
my_function()
Demonstrates triple double quot...
Please Login to comment...