Python help() Function: How It Works

Quick answer: Call help() interactively with no argument for the help system, or pass a module, class, function, method, keyword, or string topic. For scripts and automation, inspect and pydoc often provide more controllable output.

Python help function infographic comparing interactive help, object help, module help, and inspect signatures
help() opens Python’s built-in documentation path; pass the object or module you need, and use inspect when you need machine-readable details.

Python’s help() function prints documentation for objects, modules, classes, functions, methods, and keywords. It is most useful when you are already inside a Python shell and need a quick reminder without leaving the interpreter.

The official Python documentation covers the built-in help() function, the pydoc module, and the inspect module.

Call help() with no argument to start the interactive help system. Call it with an object, name, or keyword string when you want documentation for one specific target.

The output comes from docstrings and Python’s documentation tools. For your own code, that means good docstrings make help() much more useful. For built-ins and standard-library modules, it gives a concise reference for signatures and behavior.

Use help() during exploration, debugging, and teaching. Use official documentation or source code when you need complete details, examples, release notes, or edge-case behavior.

In scripts and tests, avoid depending on the exact text printed by help(). Documentation text can change between Python releases. If code needs docstrings programmatically, use inspect.getdoc() or pydoc.render_doc().

The examples below are safe to run as scripts, but the same calls are usually typed directly in an interactive Python prompt.

help() is not a linter, debugger, or type checker. It tells you what documentation is available. If you need to inspect available attributes first, combine it with dir(), then pass the most relevant attribute or method into help().

When help output is too broad, narrow the target. For example, help(math) shows a full module reference, while help(math.sqrt) focuses on one function. Focused help is easier to read and easier to compare with the code you are writing.

For your own projects, docstrings should explain purpose, inputs, return values, and important side effects. Short, direct docstrings make help() useful without forcing readers to open the source file.

For installed packages, help(package) can confirm that the import works and reveal top-level documentation. Still, package websites or official docs are usually better for longer tutorials, installation notes, and version-specific examples.

Inspect A String Method

Pass a method object to see its short documentation.

help(str.upper)

This prints documentation for the string method that converts text to uppercase.

The first lines show the object type and signature.

The following lines explain what the method returns.

This pattern is useful when you remember a method name but want a quick reminder of its arguments or return value.

Inspect A Built-In Function

Built-in functions also expose useful help text.

help(len)

The output shows that len() returns the number of items in a container.

For built-ins, help() often includes the call signature and a short description.

This is faster than searching the web for basic reminders while you are already in a Python session.

Read Keyword Help

Pass a keyword as a string to read language reference help.

help("for")

This prints documentation for the for statement.

Keyword help is useful when learning control flow, exception handling, imports, and other language syntax.

Because this output can be long, use it as a reference, not as text to parse in a program.

Inspect A Module Member

For modules, pass a specific member when you want focused output.

import math

help(math.sqrt)

This prints documentation for math.sqrt() instead of the entire math module.

Use help(math) when you want the broader module listing.

Use a focused member such as math.sqrt when you already know which function you need.

Document Your Own Function

help() reads docstrings from your own functions.

def greet(name):
    """Return a friendly greeting."""
    return f"Hello, {name}"

help(greet)

The docstring appears in the help output.

This makes docstrings useful for teammates, notebooks, command-line exploration, and future maintenance.

Keep the first docstring sentence direct. It is often the first line people see in help output.

Use help With Classes And pydoc

Classes show their class docstring and method docstrings.

import inspect
import pydoc

class Cart:
    """Store items selected by a shopper."""

    def add(self, item):
        """Add one item to the cart."""
        print(f"Added {item}")

help(Cart)
print(inspect.getdoc(Cart.add))
text = pydoc.render_doc(str.strip, renderer=pydoc.plaintext)
print(text.splitlines()[0])

The class help output includes the class description and its documented method.

inspect.getdoc() returns cleaned docstring text when you need it in code.

pydoc.render_doc() can render documentation as text without entering the interactive help system.

In short, use help() for quick interactive reference, write docstrings so your own functions and classes produce useful output, pass focused objects for shorter help, and use inspect or pydoc when documentation text is needed programmatically.

Inspect Objects Interactively

The most useful form is help(object), because it shows the object’s docstring and related members through Python’s interactive pager. You can ask for a module, built-in type, method, or installed package that is importable in the current environment.

help(str.split)
help("keywords")
help("modules")

Use The Right Tool In A Script

help() is designed for human exploration and may open a pager or print a large amount of text. For a machine-readable docstring, read __doc__; for a callable signature, use inspect.signature when the object exposes one. pydoc can render documentation without manually navigating the pager.

import inspect
import math

print(math.sqrt.__doc__)
print(inspect.signature(math.isclose))

Read Documentation Without Guessing

help() describes the object available in the interpreter, so confirm that you imported the package and version you intend to use. When behavior depends on a third-party release, pair interactive exploration with the package’s official documentation and tests.

from pathlib import Path

print(Path.exists.__doc__)
print(Path.__module__)
print(Path.__qualname__)

Python’s built-in help() documentation explains interactive help topics, while inspect.signature is the programmatic signature tool.

For deeper runtime exploration, compare importing classes, standard-library modules, and conditional imports when investigating an unfamiliar codebase.

Frequently Asked Questions

How do I use help() in Python?

Call help() in an interactive interpreter, or pass an object such as help(str), help(str.split), or help(‘keywords’).

Can I use help() inside a Python script?

Yes, but it is primarily interactive and writes documentation through the active pager or standard output; inspect or pydoc may be better for automation.

How do I exit the help screen?

Press q in the interactive pager to return to the interpreter.

What should I use when I need a function signature?

Use inspect.signature for a runtime signature when available, and read the object’s __doc__ attribute for a programmatic docstring.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted