help() function in Python is a built-in function that provides information about modules, classes, functions and modules. It is useful for retrieving information on various Python objects. Example:
Python
OutputWelcome to Python 3.13's help utility! If this is your first time using
Python, you should definitely check out the tutorial at
https://docs.python.org/3.13/tutorial/
Enter the name of any module, k...
Explanation: help() retrieves documentation using an object's __doc__ attribute. Without arguments, it starts an interactive help session, with an object, it displays method descriptions and parameters. If no documentation exists, it returns "No documentation found."
Syntax of help()
help([object])
Parameters:
- object (Optional): The module, class, function, or keyword to get information about.
- If used without an argument, it opens an interactive help session in the console.
Returns: It does not return any value, it simply prints the documentation to the console. If you try to assign its output to a variable, it will return None.
Examples of help()
Example 1. Getting Help on Built-in Functions
Python
OutputHelp on built-in function print in module builtins:
print(*args, sep=' ', end='\n', file=None, flush=False)
Prints the values to a stream, or to sys.stdout by default.
sep
string inser...
Explanation: help(print) shows its parameters (value, sep, end, file, flush) and their default values. The function prints values to sys.stdout by default, separates them with sep, ends with end, writes to a specified file and can force flushing with flush.
Example 2. Help on user-defined classes
Python
class Helper:
def __init__(self):
'''The helper class is initialized'''
def print_help(self):
'''Returns the help description'''
print('helper description')
help(Helper)
help(Helper.print_help)
OutputHelp on class Helper in module __main__:
class Helper(builtins.object)
| Methods defined here:
|
| __init__(self)
| The helper class is initialized
|
| print_help(self)
| Returns ...
Explanation: Helper class has an __init__ method with a docstring and a print_help method that prints a description. help(Helper) shows class details, while help(Helper.print_help) displays method-specific info.
Example 3. when no documentation is available
Python
print(help("GeeksforGeeks"))
OutputNo Python documentation found for 'GeeksforGeeks'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
None
Explanation: help("GeeksforGeeks") call attempts to find documentation for the string "GeeksforGeeks". Since no such module or keyword exists, it returns "No Python documentation found", suggesting help() for interactive help or help(str) for string-related documentation.
Example 4. If a string is given as an argument
Python
OutputNo Python documentation found for 'gfg'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
Example 5. Using __doc__ for documentation
Python
def fun():
"""This function demonstrates docstrings."""
return None
print(fun.__doc__)
OutputThis function demonstrates docstrings.
Explanation: fun() has a docstring describing its purpose. Calling fun.__doc__ retrieves and prints this docstring.