Get all object attributes in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting all attributes of an object in Python is useful for debugging, serialization, and dynamic inspection. The primary tools are dir(), which lists all attribute names including inherited and special ones, and vars(), which returns the instance's __dict__ containing only its own attributes. For more detailed inspection, the inspect module provides functions to distinguish between methods, properties, and data attributes. Understanding these tools helps you explore unfamiliar objects and build dynamic, reflection-based code.
Using dir()
dir() returns a sorted list of all attribute names for an object, including inherited attributes and special (dunder) methods:
To filter out dunder attributes:
Using vars()
vars() returns the __dict__ of an object — only the instance attributes, not class attributes or methods:
Notice that species (class attribute) and speak (method) are not in vars() because they belong to the class, not the instance.
Comparing dir() and vars()
| Feature | dir() | vars() |
| Instance attributes | Yes | Yes |
| Class attributes | Yes | No |
| Methods | Yes | No |
| Inherited attributes | Yes | No |
| Special methods | Yes | No |
| Returns | List of strings | Dictionary |
Using getattr() for Dynamic Access
Using the inspect Module
The inspect module provides detailed attribute classification:
Filtering by Type
Using slots
Objects with __slots__ do not have __dict__, so vars() fails:
Practical Example: Object to Dictionary
Common Pitfalls
- Assuming
vars()shows all attributes:vars()only returns instance attributes stored in__dict__. Class attributes, methods, properties, and__slots__attributes are not included. Usedir()for a complete list. - Using
vars()on objects with__slots__: Objects with__slots__do not have__dict__, sovars()raisesTypeError. Check withhasattr(obj, '__dict__')first, or usedir()andgetattr(). - Modifying
__dict__directly: Whileobj.__dict__["new_attr"] = valueworks, it bypasses descriptors, properties, and__setattr__. Usesetattr(obj, "new_attr", value)for safe attribute setting. - Confusing class attributes with instance attributes:
dir()shows both,vars()shows only instance attributes. If you modify a class attribute through an instance (obj.class_var = new_value), it creates an instance attribute that shadows the class one. - Not filtering dunder attributes:
dir()includes dozens of special methods (__init__,__repr__,__eq__, etc.). Filter with[a for a in dir(obj) if not a.startswith("_")]to see only user-defined attributes.
Summary
dir(obj)lists all attribute names including inherited and special attributesvars(obj)returns__dict__with instance attributes only (fails on__slots__objects)getattr(obj, name, default)accesses attributes dynamically by nameinspect.getmembers(obj)provides detailed classification of methods, properties, and data- Filter
dir()results withnot a.startswith("_")to see only public, user-defined attributes
Related reading
- Get AWS Account ID from Boto
- Get column index from column name in python pandas
- Get column name based on condition in pandas
- Get confidence interval from sklearn linear regression in python
- Get first element from a dictionary
- Get fully qualified class name of an object in Python
- Get HTML source of WebElement in Selenium WebDriver using Python
- Get IP address of visitors using Flask for Python
.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.