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.
import math
math?
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_methodAccessing 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
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)
File: /usr/lib/python3.10/os.py
Type: functionLimitation: 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.
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_methodUsing 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.
help(len)Output:
Help on built-in function len in module builtins:
len(obj, /)
Return the number of items in a container.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 npResult: IPython will suggest:
np.linalgnp.linspace
Using dir()
To list all valid attributes and methods of an object programmatically, use dir().
import datetime
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.
- Use
obj?for quick access to docstrings and signatures. - Use
obj??to inspect the actual Python source code. - Use Tab Completion to explore available methods dynamically.
- Use
help(obj)as a fallback or for standard Python environments.