introspection
In Python, introspection is the ability to examine an object’s type and properties at runtime. It lets you discover relevant information, such as an object’s class, attributes, and methods, when you don’t know its exact structure ahead of time. This is useful for debugging, testing, and building dynamic features.
Python supports quick introspection with built-ins like type(), dir(), vars(), and getattr().
For deeper inspection, like examining call signatures, fetching source code, or exploring the call stack, you can use Python’s inspect module, which provides specialized helpers for working with live objects.
Example
Here’s a quick example of using introspection to examine an object in Python:
>>> class Dog:
... def __init__(self, name):
... self.name = name
...
... def speak(self):
... return "Woof Woof"
...
>>> buddy = Dog("Buddy")
>>> # Introspection
>>> type(buddy)
<class '__main__.Dog'>
>>> dir(buddy)
['__class__', '__delattr__', '__dict__', ..., 'name', 'speak']
>>> vars(buddy)
{'name': 'Buddy'}
In this example, you can see how to use type(), dir(), and vars() to gather information about the buddy object and its properties.
Related Resources
Tutorial
Python's Built-in Exceptions: A Walkthrough With Examples
In this tutorial, you'll get to know some of the most commonly used built-in exceptions in Python. You'll learn when these exceptions can appear in your code and how to handle them. Finally, you'll learn how to raise some of these exceptions in your code.
For additional information on related topics, take a look at the following resources:
By Leodanis Pozo Ramos • Updated Jan. 9, 2026