inspect

The Python inspect module provides several useful tools to help you get information about live objects, such as modules, classes, methods, functions, tracebacks, and the objects’ source code.

It allows you to inspect the types and properties of these objects, retrieve source code, and get information on the calling context.

Here’s an example:

Python
>>> import inspect

>>> def example_function():
...     pass
...

>>> inspect.isfunction(example_function)
True

Key Features

  • Retrieves the source code of objects
  • Inspects classes, functions, and methods
  • Examines the stack frame
  • Analyzes the arguments of callable objects
  • Retrieves information about the calling context

Frequently Used Classes and Functions

Object Type Description
inspect.getsource() Function Returns the text of the source code for an object
inspect.getmembers() Function Returns all members of an object as name-value pairs
inspect.isfunction() Function Checks if an object is a function
inspect.signature() Function Gets a callable’s signature
inspect.getdoc() Function Retrieves the docstring of an object
inspect.isclass() Function Returns True if the object is a class, whether built-in or created in Python code
inspect.currentframe() Function Returns the current stack frame

Examples

Retrieving the source code of a function:

Python
>>> import inspect

>>> def example_function():
...     """This is an example function."""
...     pass

>>> print(inspect.getsource(example_function))
def example_function():
    """This is an example function."""
    pass

Checking if an object is a class:

Python
>>> class ExampleClass:
...     pass
...

>>> inspect.isclass(ExampleClass)
True

Getting the signature of a function:

Python
>>> def example_function(a, b=2, *args, **kwargs):
...     pass
...

>>> inspect.signature(example_function)
<Signature (a, b=2, *args, **kwargs)>

Common Use Cases

  • Retrieving and displaying the source code of functions and classes
  • Analyzing function signatures and arguments
  • Debugging and understanding the stack trace
  • Generating documentation or interactive help

Real-World Example

Suppose you want to create a simple command-line tool that inspects functions within a module and displays their signatures and docstrings. Here’s a way to achieve that:

Python
>>> import inspect
>>> import custom_module

>>> for name, obj in inspect.getmembers(custom_module, inspect.isfunction):
...     print(f"Function: {name}")
...     print(f"Signature: {inspect.signature(obj)}")
...     print(f"Docstring: {inspect.getdoc(obj)}\n")
...

This example demonstrates how to list all functions in a custom module, showing their signatures and docstrings, which can be particularly useful for documentation purposes or understanding a codebase.

Tutorial

Serialize Your Data With Python

In this in-depth tutorial, you'll explore the world of data serialization in Python. You'll compare and use different data serialization formats, serialize Python objects and executable code, and handle HTTP message payloads.

intermediate data-science web-dev

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated July 10, 2025