vars()

vars([object])
Returns: dict · Updated March 13, 2026 · Built-in Functions
built-in introspection attributes dictionary

The vars() function returns the __dict__ attribute of a given object, or the current local symbol table as a dictionary. When called without arguments, it behaves like locals(). When called with an object, it returns that object’s __dict__, which contains all the instance attributes.

Syntax

vars([object])

Parameters

ParameterTypeDefaultDescription
objectobjectAny object with a __dict__ attribute. If omitted, returns the local namespace.

Examples

Basic usage with a custom object

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("Alice", 30)
print(vars(person))
# {'name': 'Alice', 'age': 30}

Using vars() without arguments

x = 10
y = "hello"
print(vars())
# {'__name__': '__main__', ..., 'x': 10, 'y': 'hello'}

Inspecting module attributes

import os
print(vars(os))
# {'__name__': 'posix', '__file__': '/usr/lib/python3.11/os.py', ...}

Common Patterns

Debugging object state

The most common use for vars() is debugging. When you want to inspect what attributes an object currently holds:

class Config:
    def __init__(self):
        self.debug = True
        self.port = 8080

config = Config()
print("Current config:", vars(config))
# Current config: {'debug': True, 'port': 8080}

Copying object state

You can use vars() to create a shallow copy of an object’s attributes:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p1 = Point(1, 2)
p2 = Point(**vars(p1))  # unpacks {'x': 1, 'y': 2}
print(p2.x, p2.y)
# 1 2

Comparing object states

class User:
    def __init__(self, name):
        self.name = name
        self.created_at = "2024-01-01"

user1 = User("Alice")
user2 = User("Alice")

print(vars(user1) == vars(user2))
# True (both have same name and created_at)

Errors

  • TypeError: If the object has no __dict__ attribute. For example, vars(42) raises TypeError because integers don’t have a __dict__.
  • Most built-in types (int, str, list, tuple) do not have __dict__ and will raise TypeError.

See Also