Last modified April 11, 2025
This comprehensive guide explores Python's vars function, which
returns the __dict__ attribute of an object. We'll cover objects,
modules, namespaces, and practical examples of attribute introspection.
The vars function returns the __dict__ attribute of
an object when called with an argument. Without arguments, it behaves like
locals().
Key characteristics: works with objects having __dict__ attribute,
modules, and classes. Returns a dictionary of namespace symbols when called
without arguments.
Here's simple usage with different objects showing how vars
accesses their attributes through the __dict__ attribute.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("John", 30)
print(vars(p)) # {'name': 'John', 'age': 30}
# Adding a new attribute
p.job = "Developer"
print(vars(p)) # {'name': 'John', 'age': 30, 'job': 'Developer'}
This example shows vars with a custom object. It returns a
dictionary of instance attributes. When we add a new attribute, it appears
in subsequent vars calls.
The output matches the object's __dict__ attribute, which stores
the instance's writable attributes.
vars can inspect module namespaces, showing all defined symbols.
This example demonstrates module introspection.
import math
# Get module attributes
math_vars = vars(math)
print("PI:", math_vars['pi']) # PI: 3.141592653589793
print("Square root function:", math_vars['sqrt']) # <built-in function sqrt>
# Filter constants only
constants = {k:v for k,v in math_vars.items() if not callable(v)}
print("Math constants:", constants.keys())
This shows how vars can access a module's namespace. We can
retrieve specific values or filter attributes by type.
Module namespaces contain both functions and constants, which we can separate using dictionary comprehension.
vars can inspect class attributes, showing both class-level and
instance-level attributes when used properly.
class Vehicle:
wheels = 4 # Class attribute
def __init__(self, color):
self.color = color # Instance attribute
print("Class vars:", vars(Vehicle)) # Includes wheels
car = Vehicle("red")
print("Instance vars:", vars(car)) # Only color
The example demonstrates the difference between class and instance attributes.
Class attributes appear in vars(Vehicle), while instance attributes
appear in vars(car).
This distinction is important when working with class hierarchies and attribute lookup chains.
When called without arguments, vars returns the local namespace,
similar to locals(). This example shows the behavior.
def test_function():
x = 10
y = 20
print("Local vars:", vars())
test_function() # Shows x and y in local namespace
# At module level
a = 100
b = 200
print("Module vars:", vars()) # Shows a, b, and other module-level names
This demonstrates vars() returning the current local namespace.
In functions, it shows local variables. At module level, it shows global
variables.
The behavior is identical to locals() but less commonly used in
this form.
vars raises TypeError when used with objects that
don't have __dict__. This example shows proper error handling.
# Built-in types typically don't have __dict__
try:
print(vars(10))
except TypeError as e:
print(f"Error: {e}") # vars() argument must have __dict__ attribute
class NoDict:
__slots__ = ['x'] # Uses slots instead of __dict__
try:
nd = NoDict()
print(vars(nd))
except TypeError as e:
print(f"Error: {e}") # vars() argument must have __dict__ attribute
These examples demonstrate vars's behavior with unsupported types.
Built-in types and classes with __slots__ typically don't support
vars.
To check if an object supports vars, you can use
hasattr(obj, '__dict__').
vars is great for inspecting objects during developmentdir() shows all attributes while vars shows the __dict__varsMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.