Skip to main content

How to Access Built-in Function Documentation in Python

Python is known for its "batteries included" philosophy, offering a vast standard library and numerous built-in functions like print(), len(), and sorted(). However, remembering every parameter and behavior for these functions is impossible.

This guide explains how to access Python's built-in documentation directly from your code or interactive shell using the help() function and the __doc__ attribute.

Using the Interactive help() Function

The most direct way to read documentation is the built-in help() function. It invokes the interactive help system, which formats the text, arguments, and descriptions for readability.

Correct Usage

You must pass the function object itself to help(), not the result of calling the function.

# ✅ Correct: Pass the function name without parentheses
help(print)

Output (Truncated):

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.
...

Common Mistake: Calling the Function

If you add parentheses (), Python executes the function first and then asks for help on the return value.

# ⛔️ Incorrect: This executes print(), which returns None, 
# so you get help on the 'NoneType' object instead of the print function.
help(print())

Output:

Help on NoneType object:

class NoneType(object)
| Methods defined here:
|
| __bool__(self, /)
| True if self else False
...
tip

To exit the help viewer in a terminal or command prompt, press the q key.

Accessing Raw Docstrings via __doc__

Every Python object has a __doc__ dunder (double underscore) attribute. This attribute contains the raw documentation string (docstring). This is useful if you want to print the documentation programmatically without entering the interactive help mode.

# ✅ Correct: Accessing the __doc__ attribute
doc_string = len.__doc__
print(doc_string)

Output:

Return the number of items in a container.
note

The __doc__ attribute contains the raw text exactly as it was written in the source code. help() cleans this up and adds metadata (like the module name).

Understanding Function Signatures

When you access documentation, the most critical part is the Signature, i.e. the line defining the parameters.

Example signature from help(sorted): sorted(iterable, /, *, key=None, reverse=False)

  • iterable: A required argument (e.g., a list).
  • /: Arguments before this must be positional only.
  • *: Arguments after this must be keyword arguments (named).
  • reverse=False: An optional argument with a default value.
# Based on the documentation for 'sorted' found via help(sorted)

data = [3, 1, 2]

# ✅ Correct: Using keyword arguments as described in docs
sorted_data = sorted(data, reverse=True)
print(sorted_data)

Output:

[3, 2, 1]

Conclusion

To effectively use Python's built-in functions:

  1. Use help(function_name) in the interactive shell for a readable, detailed manual.
  2. Use print(function_name.__doc__) for a quick, programmatic check of what a function does.
  3. Do not call the function inside help(); pass the object directly.