Skip to main content

How to Access Documentation for Python Objects in IPython

IPython (Interactive Python) is more than just a command shell; it is a powerful tool for exploration. One of its strongest features is the ability to access documentation, docstrings, and even source code without ever leaving the terminal or Jupyter Notebook. This significantly speeds up development by reducing context switching between your code and a browser.

This guide explains the most efficient ways to inspect Python objects, retrieve help text, and explore attributes directly within IPython.

Using the Question Mark Operator (?)

The most distinct and frequently used feature in IPython is the single question mark ?. Appending this to any object, function, or method displays its docstring (documentation string), calling signature, and other metadata.

1.1 Inspecting Functions and Modules

Simply type the object name followed by ? and hit Enter.

# In an IPython shell or Jupyter Notebook
import math

# ✅ Inspect the math module
math?

# ✅ Inspect a specific function
print?

Output (Simulated Pager View):

Docstring:
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.
...
Type: builtin_function_or_method
tip

You can also place the question mark before the object (e.g., ?print). Both print? and ?print achieve the same result.

Accessing Source Code (??)

If the docstring isn't enough and you want to see how a function is implemented, use double question marks ??. This attempts to retrieve and display the actual source code of the object.

import os

# ✅ View the source code of the os.makedirs function
os.makedirs??

Output:

Signature: os.makedirs(name, mode=511, exist_ok=False)
Source:
def makedirs(name, mode=0o777, exist_ok=False):
"""makedirs(name [, mode=0o777][, exist_ok=False])

Super-mkdir; create a leaf directory and all intermediate ones. Works like
mkdir -p except that the mode parameter is passed to...
"""
head, tail = path.split(name)
if not tail:
head, tail = path.split(head)
if head and tail and not path.exists(head):
try:
makedirs(head, mode, exist_ok)
# ... (rest of the source code)
File: /usr/lib/python3.10/os.py
Type: function

Limitation: C-Extensions

Standard Python functions (written in .py files) will show their source code. However, built-in functions written in C (like len or sum) cannot display source code because it is compiled.

# ⛔️ Limitation: 'sum' is a built-in C function
sum??

Output:

Signature: sum(iterable, /, start=0)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers...
Type: builtin_function_or_method
note

Note that the Source: section is missing because the source is not Python code.

Using the Standard help() Function

If you are using a standard Python shell (not IPython) or writing a script where you need to print help text programmatically, use the built-in help() function. While less interactive than ?, it is universally available.

# ✅ Works in standard Python and IPython
help(len)

Output:

Help on built-in function len in module builtins:

len(obj, /)
Return the number of items in a container.
note

In IPython, help(obj) allows you to scroll through the documentation in the main output area, whereas obj? typically opens a separate pager at the bottom of the screen (in Jupyter) or a scrollable overlay (in terminal).

Discovering Attributes with Tab Completion and dir()

Before you can read the documentation, you often need to find the specific function or attribute name.

Tab Completion

IPython's most productivity-enhancing feature is Tab Completion. Type the name of an object, a dot ., and then press Tab.

import numpy as np

# Type 'np.lin' and press Tab
# np.lin<TAB>

Result: IPython will suggest:

  • np.linalg
  • np.linspace

Using dir()

To list all valid attributes and methods of an object programmatically, use dir().

import datetime

# ✅ List all attributes of the datetime class
print(dir(datetime.datetime))

Output:

['__add__', '__class__', ..., 'now', 'strptime', 'today', 'utcnow', ...]

Conclusion

Mastering documentation access in IPython enables you to write code faster and with fewer errors.

  1. Use obj? for quick access to docstrings and signatures.
  2. Use obj?? to inspect the actual Python source code.
  3. Use Tab Completion to explore available methods dynamically.
  4. Use help(obj) as a fallback or for standard Python environments.