La fonction help en Python

La fonction help permet d'afficher la documentation d'un objet Python.

On peut utiliser cette fonction sans lui passer d'argument dans un interpréteur Python pour lancer l'aide intéractive :

>>> help()

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> 

Quand vous passez un objet en argument à la fonction help, elle vous retourne le docstring (DOCumentation STRING) de l'objet.

Si l'objet en question ne contient pas de documentation (aucune docstring), la fonction help ne vous retournera donc rien.

Par exemple si je fais une fonction foo sans docstring :

def foo():
    pass

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

foo()

Avec une fonction documentée, la fonction help nous retourne le docstring de la fonction passée en argument :

def foo():
    """Cette fonction ne sert à rien"""
    pass
>>> help(foo)
Help on function foo in module __main__:

foo()
    Cette fonction ne sert à rien

On peut utiliser cette fonction sur n'importe quel type d'objet, par exemple sur un module entier :

import random
help(random)

Ou un objet natif comme un nombre entier :

help(5)