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
Course
Working With Python's .__dict__ Attribute
Explore Python's .__dict__ attribute to manage class and instance attributes directly for more flexible, low-level control of your objects.
For additional information on related topics, take a look at the following resources:
- Using Python's .__dict__ to Work With Attributes (Tutorial)
- Python's Built-in Exceptions: A Walkthrough With Examples (Tutorial)
- Using Python's .__dict__ to Work With Attributes (Quiz)
- Working With Python's Built-in Exceptions (Course)
- Python's Built-in Exceptions: A Walkthrough With Examples (Quiz)
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. 7, 2026