Python vars() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

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()
CallWhat 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

ParameterDescription
objectOptional. A module, class, instance, or any object with a __dict__.

Return Value

Return ValueDescription
dictThe 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__)

How It Works

  1. Attribute dictionary. Each self.x = ... in __init__ becomes a key.
  2. Insertion order. Keys appear in the order the attributes were assigned.
  3. Not a copy. vars(b) is the very same dict as b.__dict__.
  4. 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)

How It Works

  1. Write-through. Adding a key to vars(p) creates the attribute p.z.
  2. Copy for export. dict(vars(p)) builds a separate dictionary.
  3. Original untouched. Changing the copy leaves p.x at 1.
  4. 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())

How It Works

  1. Module namespace. vars(math) maps every name defined in the module, so membership tests work without printing it all.
  2. No dict, no vars. Ints and slotted instances raise TypeError.
  3. No argument. Inside a function, vars() returns the local names, like locals().
  4. 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.

Next: zip()

Pair up items from several iterables.

zip() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful