Skip to content

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:

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

Using Python's .__dict__ to Work With Attributes

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.

advanced 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. 7, 2026