In Python, vars(obj) returns obj.__dict__, the dictionary in which an object keeps its attributes. For a simple class instance, that is a dict of attribute names to values, which makes it handy for debugging and simple serialization.
For instances, it is the live dictionary, not a copy: changing it changes the object. With no argument, vars() behaves like locals(). Objects without a __dict__, such as ints or classes that use __slots__, raise TypeError.
vars() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass an object, or nothing for the current local scope.
python
attributes = vars(obj)
current = vars()
Call
What it does
vars(p)
The instance's attributes as a dict.
vars(p) is p.__dict__
True for normal instances.
dict(vars(p))
An independent copy.
vars(42)
Raises TypeError.
vars() shows only what is stored on the object itself, not class attributes or methods; dir() lists those too.
Library declarations
python
vars()
vars(object, /)
Parameters
Parameter
Description
object
Optional. A module, class, instance, or any object with a __dict__.
Return Value
Return Value
Description
dict
The object's __dict__ (for classes, a read-only mapping proxy).
Python Examples
Example 1 — See an Object's Attributes as a dict
Print the attribute dictionary of a simple instance.
python
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
b = Book("Dune", 412)
print(vars(b))
print(vars(b) is b.__dict__)
Output
{'title': 'Dune', 'pages': 412}
True
How It Works
Attribute dictionary. Each self.x = ... in __init__ becomes a key.
Insertion order. Keys appear in the order the attributes were assigned.
Not a copy.vars(b) is the very same dict as b.__dict__.
Debugging aid. Printing it is a quick way to inspect an object's state.
Example 2 — Change the Object Through vars()
Add an attribute through the dictionary, then make a safe copy for export.
python
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
p = Point(1, 2)
vars(p)["z"] = 3
print(p.z)
data = dict(vars(p))
data["x"] = 99
print(p.x, data)
Output
3
1 {'x': 99, 'y': 2, 'z': 3}
How It Works
Write-through. Adding a key to vars(p) creates the attribute p.z.
Copy for export.dict(vars(p)) builds a separate dictionary.
Original untouched. Changing the copy leaves p.x at 1.
Simple serialization. The copy can be passed to json.dumps() or saved.
Example 3 — Modules, Missing __dict__, and vars() Without Arguments
Look inside a module, try objects without __dict__, and list a function's local names.
python
import math
class Slotted:
__slots__ = ("a",)
print("pi" in vars(math), "sqrt" in vars(math))
for obj in [42, Slotted()]:
try:
vars(obj)
except TypeError:
print(type(obj).__name__, "has no __dict__")
def show():
local_value = 10
return sorted(vars())
print(show())
Output
True True
int has no __dict__
Slotted has no __dict__
['local_value']
How It Works
Module namespace.vars(math) maps every name defined in the module, so membership tests work without printing it all.
No dict, no vars. Ints and slotted instances raise TypeError.
No argument. Inside a function, vars() returns the local names, like locals().
Selected keys. Printing sorted names keeps the output short and predictable.
Frequently Asked Questions
The __dict__ of the object: a dictionary of its attributes. Without an argument, the local namespace.
vars() returns a dict of attributes stored on the object. dir() returns a sorted list of names, including inherited ones and methods.
dict(vars(obj)) gives a copy of its instance attributes. For dataclasses, dataclasses.asdict() also handles nesting.
The object has no __dict__, for example ints, strings, or classes that use __slots__.
For normal instances, yes: vars(obj) is the live __dict__. Class mappings are read-only.