Skip to content

core_ipython_features

Core IPython Features (Completion, Introspection)

Section titled “Core IPython Features (Completion, Introspection)”

Beyond basic code execution, IPython provides powerful features that significantly enhance productivity during interactive sessions. Two fundamental features are Tab Completion and Object Introspection.

IPython offers context-aware completion of code elements by pressing the Tab key. This works for:

  • Variable Names: Complete names of variables already defined in the namespace.
  • Keywords: Complete Python keywords (like import, for, def).
  • Object Attributes and Methods: After typing an object name followed by a dot (.), hitting Tab shows a list of available attributes and methods for that object.
  • Function Keyword Arguments: Inside a function call, Tab can suggest available keyword arguments.
  • File Paths: When typing strings that look like paths, Tab attempts to complete file and directory names.
  • Magic Commands: Complete names of magic commands after typing %.

Example:

In [1]: my_variable = 100
In [2]: import numpy as np
In [3]: my_arr = np.array([1, 2, 3])
# Try these in an IPython cell:
# my_var<Tab> --> completes to my_variable
# np.ar<Tab> --> suggests array, arange, etc.
# my_arr.<Tab> --> shows methods like .sum(), .mean(), attributes like .shape, .dtype
# np.array? --> (See Introspection below)
# np.array(obj<Tab> --> suggests object=..., dtype=..., copy=..., etc.
# %his<Tab> --> completes to %history

Tab completion dramatically reduces typing, helps prevent typos, and serves as a discovery mechanism for object capabilities.

Introspection is the ability to examine the properties of Python objects during runtime. IPython provides a convenient syntax using the question mark (?) to access documentation and information about objects.

  • object?: Shows information like the object’s type, docstring, function signature (if applicable), and file location.
  • object??: Provides more detailed information, including the source code of the object if available (e.g., for functions or classes defined in Python).

This works for variables, functions, modules, classes, methods, etc.

In [4]: import pandas as pd
# Introspect a variable
In [5]: my_arr?
# Output:
# Type: ndarray
# String form: [1 2 3]
# Length: 3
# ... (more info)
# Introspect a function
In [6]: pd.read_csv?
# Output: Shows the function signature, extensive docstring explaining parameters, examples etc.
# Introspect a module
In [7]: pd?
# Output: Shows module docstring, file path, etc.
# Get source code for a function (if available)
In [8]: def my_function(a, b=1):
...: """This is my custom function."""
...: return a + b
...:
In [9]: my_function??
# Output:
# Signature: my_function(a, b=1)
# Docstring: This is my custom function.
# Source:
# def my_function(a, b=1):
# """This is my custom function."""
# return a + b
# File: ...
# Type: function

Wildcard Introspection: You can also use wildcards (*) with ? to search the namespace.

In [10]: *Warning? # Find all names ending with 'Warning'
# Output: UserWarning, DeprecationWarning, RuntimeWarning, etc.

Introspection using ? and ?? is incredibly useful for quickly checking how to use a function, understanding what an object is, or even looking at the source code without leaving your interactive session. It significantly aids exploration and debugging.

These core features, tab completion and object introspection, are fundamental to the fluid and efficient workflow provided by IPython and Jupyter.