Python help() Example | help() Function In Python Tutorial
Python help() is an inbuilt method that is used for interactive use. The help() function is used to get documentation of the specified module, class, function, variables, etc. The help() method is generally used with the python interpreter console to get details about python objects.
Python help() Method
It’s recommended to try it in your interpreter when you need help to write the Python program and use Python modules.
Okay, now go to the terminal or CMD console and type the following command.
Right now, Python3 is the latest, so type the python3, and you will see like below.
➜ pyt python3 Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Okay, now type the help() function.
You will see something like below.
>>> help() Welcome to Python 3.6's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.6/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>
Okay, now we can check the following inbuilt functions and modules.
help> collections help> modules help> builtins help> symbols help> topics help> keywords help> LOOPING
Let’s say we type the collections, and then we get the following output in the console.
Help on package collections: NAME collections MODULE REFERENCE https://docs.python.org/3.6/library/collections The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above. DESCRIPTION This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict, list, set, and tuple. * namedtuple factory function for creating tuple subclasses with named fields * deque list-like container with fast appends and pops on either end * ChainMap dict-like class for creating a single view of multiple mappings :
The help() method takes the maximum of one parameter.
#An object is passed to help()
Type the following command.
See the following output.
Help on class list in module builtins: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) :
#String is passed as an argument
If the string is passed as an argument, the given string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed.
See the following command.
See the following 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. (END)
If you want to quit the help console, type quit command.
#Defining help() for custom class and functions
We can define the help() function output for our custom classes and functions by defining the docstring (documentation string).
By default, the first comment string in a body of the method is used as its docstring. Three double-quotes surround it.
Let’s say we have a python file help_examples.py with the following code.
def add(a, b): """ This function adds the given integer arguments :param a: integer :param b: integer :return: integer """ return a + b class Student: """ Student class, mapped to "student" table in Database """ enrollno = 0 name = '' def __init__(self, i, n): """ Student object constructor :param i: integer, must be positive :param n: string """ self.enrollno = i self.name = n
Let’s see how to get the above docstring as help the documentation in python console.
First of all, we will have to execute the above script in the console to load our function and class definition. We can do this using exec() command.
Okay, now type the following command.
>>> exec(open("help_examples.py").read()) >>> help('help_examples')
You will get the following output.
elp on module help_examples: NAME help_examples CLASSES builtins.object Student class Student(builtins.object) | Student class, mapped to "student" table in Database | | Methods defined here: | | __init__(self, i, n) | Student object constructor | :param i: integer, must be positive | :param n: string | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined)
So, we have defined a custom class and functions.
Finally, Python help() Example Tutorial is over.