How do I look inside a Python object?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python has strong introspection features, so you can inspect object attributes, methods, types, and source information at runtime. This is useful when debugging unfamiliar libraries or exploring dynamic APIs. The key is choosing the right inspection tool for the specific question you are trying to answer.
Start with type, dir, and vars
Use basic built-ins first.
typetells you the class.dirlists accessible names.varsreturns instance dictionary when available.
Inspect Instance Attributes and Class Attributes
Attributes can live on the instance or class.
Understanding this distinction helps explain shadowing and method lookup behavior.
Use inspect for Deeper Introspection
The inspect module can reveal signatures, docs, and source.
For third-party packages, source may be unavailable in some environments, but signatures and docs are often still accessible.
Check Available Methods and Their Callability
When object APIs are unclear, list callable members.
This helps discover capabilities without reading full documentation first.
Pretty Print Nested Structures
Objects frequently contain nested dictionaries and lists. Pretty printing improves readability.
For dataclasses, convert to dictionaries first.
Interactive Tips in REPL and Notebooks
In interactive sessions:
help(obj)shows docs and methods.- Tab completion reveals available names.
obj.__class__.__mro__shows inheritance chain.
These shortcuts speed up library exploration.
Safe Introspection Practices
Some properties execute code when accessed. Prefer non-invasive inspection first.
- Start with
dirandinspect.getmembersfilters. - Avoid blindly evaluating every attribute.
- Wrap inspection in try blocks for unstable objects.
This avoids debugger crashes when objects contain side-effecting descriptors.
Inspect Object Source and Inheritance
When behavior is inherited, inspect class hierarchy and method resolution order.
This clarifies where methods are defined and which implementation executes first in complex class trees.
Inspect Dictionary-Like and Slot-Based Objects
Not all objects expose __dict__. Slot-based classes need different handling.
Knowing this distinction prevents false assumptions during debugging utilities.
Common Pitfalls
- Assuming
diroutput equals only instance attributes. - Accessing properties that trigger expensive work or side effects.
- Ignoring inheritance when searching for method definitions.
- Treating dynamically generated attributes as static API contracts.
- Inspecting objects in production paths with heavy logging overhead.
Summary
- Python introspection starts with
type,dir, andvars. - Use
inspectfor signatures, docs, and source details. - Distinguish instance and class attribute storage.
- Use pretty printing and dataclass helpers for nested data.
- Inspect defensively to avoid side effects during debugging.
Related reading
- How do I loop through a list by twos?
- How do I lowercase a string in Python?
- How do I make a flat list out of a list of lists?
- How do I make function decorators and chain them together?
- How do I measure elapsed time in Python?
- How do I merge a list of dicts into a single dict?
- How do I merge two dictionaries in a single expression in Python?
- How do I merge two lists of tuples based on a key?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.