List attributes of an object
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Listing object attributes in Python is useful for debugging, introspection, and dynamic tooling. The right method depends on whether you want instance data, methods, or full class-level metadata. Using dir, vars, and inspect intentionally gives accurate and readable introspection output.
Core Sections
Quick Attribute Listing with dir
dir(obj) returns attribute names available on the object, including inherited members.
This is broad and useful for exploration, but includes magic names and methods.
Instance Attributes with vars or __dict__
If you only want instance data fields, use vars or obj.__dict__.
This is the cleanest path for serialization-like debugging.
Filter Public and Callable Members
For better readability, filter names and values explicitly.
This separates methods from data members for clearer output.
Use inspect for Richer Metadata
The inspect module helps discover functions, signatures, and class details.
This is useful for plugin systems and auto-documentation tools.
Dataclasses and Typed Models
For dataclasses, prefer dataclasses.fields to inspect declared model fields reliably.
Framework-specific models often provide their own metadata APIs, which are safer than raw introspection.
Security and Privacy Considerations
Do not dump full attributes in production logs for sensitive objects. Attributes may include tokens, personal data, or internal secrets. Build allowlists when introspection output is user-visible or exported.
For debugging endpoints, redact values and gate access behind administrative controls.
Practical Debug Utility for Safe Introspection
In production support tools, a safe introspection helper can reveal object state without exposing sensitive fields. Build a reusable filter with allowlist and optional depth limits.
This keeps diagnostics useful while reducing leakage risk.
Introspection in Dynamic Frameworks
Framework objects often override attribute resolution with descriptors or properties. In those cases, dir may show names whose values trigger lazy loading or side effects. Prefer framework APIs for metadata when available, and avoid evaluating arbitrary attributes during debug output generation.
If reflection is required, catch exceptions around getattr and mark problematic attributes rather than failing the whole inspection routine.
When building admin tools, present introspection data with clear labels for class-level and instance-level values. This reduces misinterpretation and avoids accidental edits based on incomplete context.
For maintainability, keep introspection utilities small and well-tested so behavior stays predictable across Python upgrades and framework changes.
Common Pitfalls
- Using
diroutput directly and assuming every name is instance data. - Logging entire
__dict__for sensitive domain objects. - Forgetting inherited attributes when introspection appears incomplete.
- Treating framework-generated attributes as stable public API.
- Relying on reflection-heavy code paths in performance-critical loops.
Summary
- Use
dirfor broad exploration of available attributes. - Use
varsor__dict__for instance field inspection. - Use
inspectfor method and signature metadata. - Prefer framework or dataclass metadata APIs when available.
- Apply privacy and security controls to introspection outputs.

