Skip to content

inspect

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

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:

Language: 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:

Language: 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:

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

>>> inspect.isclass(ExampleClass)
True

Getting the signature of a function:

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

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

Common Use Cases

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:

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

How to Debug Common Python Errors

Tutorial

How to Debug Common Python Errors

Learn how to debug Python errors using tracebacks, print(), breakpoints, and tests. Master the tools you need to fix bugs faster and write better code.

basics python

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

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 12, 2026