BlogsDope image BlogsDope

Python help() function

Feb. 28, 2021 PYTHON FUNCTION 870

If you are ever stuck somewhere while programming in Python, you can always seek help from Python itself. The Python help() function is a utility function that calls the built-in Python help system and it is meant for interactive use.

Python help() Syntax

The following is the syntax of the help() method:

help(object)

Python help() Parameters 

The help() method can take a maximum of one parameter. 
  • object (optional): This parameter refers to the object on which you want help.

Python help() Return Value

The help() method does not return a particular value or object, rather it is meant for interactive usage. 
  • object passed: If a particular object is passed as a parameter to the help() function, it displays documentation of modules, functions, classes, keywords etc. related to that object.
  • no object passed: If no object is passed as a parameter, then the interactive help utility starts up on the console.

Python help() Examples

Example 1:

help()

Output:

​Welcome to Python 3.8'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.8/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>

Example 2:

help(print)

Output:

Help on built-in function print in module builtins:
p.pr.ri.in.nt.t(...)
 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 3:

class CodesDope: 
    def __init__(self): 
        '''The helper class is initialized'''
  
    def myFunc(self): 
        '''Returns the help description'''
        print('helper description') 
  
help(CodesDope) 
help(CodesDope.myFunc)

Output:

​Help on class CodesDope in module __main__:

class C.Co.od.de.es.sD.Do.op.pe.e(builtins.object)

| Methods defined here:
|  
| .[4m__.[24mi.in.ni.it.t.[4m__.[24m(self)
| The helper class is initialized
|
| m.my.yF.Fu.un.nc.c(self)
| Returns the help description
|  
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| .[4m__.[24md.di.ic.ct.t.[4m__.[24m
| dictionary for instance variables (if defined)
|
| .[4m__.[24mw.we.ea.ak.kr.re.ef.f.[4m__.[24m
| list of weak references to the object (if defined)

Help on function myFunc in module __main__:
m.my.yF.Fu.un.nc.c(self)
Returns the help description

In this way we have seen the working of the Python help() function and how it can be used to seek help about any object in Python.


Liked the post?
Zealous Learner.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).