Tutorials  /  Python

Python help() function

Ccentron Redaktion · February 2024 ·4 min read ·Python, Tutorial

The Python help() function is used to get the documentation of specified modules, classes, functions, variables, etc. This method is generally used with the Python interpreter console to get details about Python objects.

Usage of Python help() Function

Python help() function syntax is:

Code
help([object])

If no argument is given, the interactive help system starts on the interpreter console.

In the Python help console, we can specify module, class, function names to get their help documentation. Some examples include:

Code
help> True
help> collections
help> builtins
help> modules
help> keywords
help> symbols
help> topics
help> LOOPING

If you want to get out of help console, type quit. We can also get the help documentation directly from the python console by passing a parameter to help() function.

Code
>>> help('collections')
>>> help(print)
>>> help(globals)

Let’s see what is the output of the help() function for the globals() function.

YAML
>>> help('builtins.globals')

Help on built-in function globals in builtins:

builtins.globals = globals()
    Return the dictionary containing the current scope's global variables.
    
    NOTE: Updates to this dictionary *will* affect name lookups in the current global scope and vice-versa.
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Defining help() for Custom Classes and Functions

We can define the help() function output for our custom classes and functions by defining a docstring (documentation string). The first comment string in the body of a method is usually used as its docstring, surrounded by three double quotes. Let’s say we have a python file python_help_examples.py with following code.

Python
def add(x, y):
    """
    This function adds the given integer arguments
    :param x: integer
    :param y: integer
    :return: integer
    """
    return x + y


class Employee:
    """
    Employee class, mapped to "employee" table in Database
    """
    id = 0
    name = ''

    def __init__(self, i, n):
        """
        Employee object constructor
        :param i: integer, must be positive
        :param n: string
        """
        self.id = i
        self.name = n

Notice that we have defined docstring for function, class and its methods. You should follow some format for documentation, I have generated some part of them automatically using PyCharm IDE. Let’s see how to get this docstring as help documentation in python console. First of all, we will have to execute this script in the console to load our function and class definition. We can do this using exec() command.

Code
>>> exec(open("python_help_examples.py").read())

We can verify that the functions and class definitions are present using globals() command.

Code
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__warningregistry__': {'version': 0}, 'add': , 'Employee': <class '__main__.Employee'>}

Notice that ‘Employee’ and ‘add’ are present in the global scope dictionary. Now we can get the help documentation using help() function. Let’s look at some of the examples.

Code
>>> help('python_help_examples')
Code
>>> help('python_help_examples.add')

Help on function add in python_help_examples:

python_help_examples.add = add(x, y)
    This function adds the given integer arguments
    :param x: integer
    :param y: integer
    :return: integer
(END)
Code
>>> help('python_help_examples.Employee')
Code
>>> help('python_help_examples.Employee.__init__')


Help on function __init__ in python_help_examples.Employee:

python_help_examples.Employee.__init__ = __init__(self, i, n)
    Employee object constructor
    :param i: integer, must be positive
    :param n: string
(END)

Summary

The Python help() function is very helpful for getting details about modules, classes, and functions. It's always the best practice to define docstrings for custom classes and functions to explain their usage.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren