Python help()

Python’s built-in help() function launches Python’s help system. Without an argument, help() starts an interactive session. With a string argument, help(name) looks up the name and prints a help page to the shell. With a non-string argument, help(object) prints a help page on object.

>>> help(list)             # Prints the documentation of list
>>> help(dict)             # Prints the documentation of dict
>>> help(int)              # Prints the documentation of int
>>> help('help')           # Prints the documentation of help()
>>> help()                 # Opens an interactive "help" session

Syntax help()

The help() object has the following syntax:

Syntax: 
help()               # Opens an interactive help session to explore multiple objects
help(string)         # Looks up the name and prints a help page to the shell
help(object)         # Prints a help page on object
ArgumentsobjectOptional. If given, prints a help page on the object. If it’s a string, it looks up the mapping name –> object and prints the help page on the object. If not given, opens an interactive help session.
Return ValueNoneReturns None.

Return Value help()

The help(object) function returns None.

Example help() without Argument

Without an argument, help() starts an interactive session.

>>> help()

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 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> string
Help on module string:

NAME
    string - A collection of string constants.

DESCRIPTION
    Public module variables:
    

In this example, you created the interactive help session and passed the name string (which is an object) within the session. Python then prints the associated documentation. You can close the interactive help session by hitting CTRL + C or type quit, followed by hitting enter.

Example help() with String Argument

With a string argument, help(name) looks up the name and prints a help page to the shell.

>>> help('help')
	       
Help on module help:

NAME
    help

DESCRIPTION
    help.py: Implement the Idle help menu.
    Contents are subject to revision at any time, without notice.
...

You may ask: where does Python look up the objects associated to each string value?

The answer is simple: with the help of namespaces, it looks up names of modules, functions, methods, variables in the Python paths. Given the name, it can then get the associated objects from the memory mapping—each name points to an object in memory.

You can read more in our full tutorials:

Example help() with Object Argument

With a non-string argument, help(object) prints a help page on object.

>>> help(print)
	       
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.

The passed function object associated to the print name also works as an input for the help() function. In this case, no look up is needed—but the documentation of the passed object is printed right away.

Python help() Function Docstring

Python’s help(object) function returns the docstring associated to the object. The docstring is a string that follows immediately after the definition of a module, function, method, or object.

Here’s an example of defining a function add() with a docstring:

def add(a, b):
    '''Calculates a+b and returns the sum.'''
    return a+b


And here’s how you can access this in your code via the help(add) function call:

>>> help(add)
Help on function add in module __main__:

add(a, b)
    Calculates a+b and returns the sum.

This way, you can write your own documentation and provide other coders access to your helpful insights when writing code snippets.

Python help() Function on Custom Class

You can also call the help() function on classes. Python will then show you the available methods and their docstrings. Here’s an example:

class Car:
    '''Representing cool cars'''

    def __init__(self, brand, color):
        '''Creates a new car with a brand and color'''
        self.brand = brand
        self.color = color


    def speed():
        '''Returns the speed of the car given brand'''
        if self.brand == 'porsche':
            return 150
        return 100


Each method and the class itself has a docstring. The output when calling the help() function on the class Car is the following:

>>> help(Car)
Help on class Car in module __main__:

class Car(builtins.object)
 |  Car(brand, color)
 |  
 |  Representing cool cars
 |  
 |  Methods defined here:
 |  
 |  __init__(self, brand, color)
 |      Creates a new car with a brand and color
 |  
 |  speed()
 |      Returns the speed of the car given brand
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Summary

Python’s built-in help() function launches Python’s help system.

  • Without an argument, help() starts an interactive session—see the following code snippet.
  • With a string argument, help(name) looks up the name and prints a help page to the shell.
  • With a non-string argument, help(object) prints a help page on object.
>>> help()

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 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> string
Help on module string:

NAME
    string - A collection of string constants.

DESCRIPTION
    Public module variables:
...

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!