Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

vars

vars() returns an object's __dict__ mapping, or behaves like locals() when called without an argument. It raises TypeError for an object that lacks __dict__.

Since
3.13

How it works

Call vars() with an object to obtain that object's __dict__ mapping. With no argument, it behaves like locals(); an object without __dict__, including an instance of a class using __slots__, causes TypeError.

Syntax

vars(*object*, /*)

Parameters

NameTypeDescription
objectobject, optionalThe object whose __dict__ attribute is returned; omit it to use locals()-equivalent behavior.

Returns

The specified object's __dict__, or the current local namespace when called without an argument.

Key points

  • With an object argument, vars() exposes that object's __dict__ mapping.
  • Module and instance dictionaries can be updateable, subject to the object's own restrictions.
  • A class's dictionary is protected by a mapping proxy rather than being directly writable.
  • The zero-argument form inspects the current local namespace using locals()-equivalent behavior.
  • Python 3.13 changed the no-argument result in line with the documented locals() behavior.

Common mistakes

  • Passing an object that has no __dict__ raises TypeError; classes defining __slots__ are one documented example.
  • The returned mapping is not uniformly writable: some objects restrict updates, and class dictionaries use a mapping proxy.

Example: Inspect object attributes

class User:
    role = "admin"

user = User()
attributes = vars(user)
print(attributes)
print(vars(User)["role"])

Shows the instance dictionary and the class dictionary returned by vars().

When to use it

Object introspection

Inspecting an object's stored attributes during debugging or introspection.

Namespace access

Reading class or instance namespace entries dynamically.

Local-scope inspection

Examining local names in a context where locals()-equivalent behavior is useful.

Interview questions

QWhat does vars(obj) return?

vars(obj) returns obj.__dict__ when that attribute exists. It works for modules, classes, instances, and other objects exposing a __dict__.

Follow-up: What happens for a slotted instance? It raises TypeError because the instance does not provide __dict__.

QWhat does vars() do with no argument?

Calling vars() without an argument has the same behavior as locals(). The no-argument result follows the documented locals() semantics.

Follow-up: How does this differ from vars(obj)? vars(obj) requires the target object's __dict__, while vars() examines the current local namespace.

QWhen can vars() raise an exception?

vars() does not create a general-purpose attribute dictionary for every object. If the supplied object lacks __dict__, Python raises TypeError.

Follow-up: Why might modifying the returned mapping fail? Some objects impose write restrictions, and class dictionaries are exposed through a mapping proxy.

  • locals()
  • __dict__
  • types.MappingProxyType

Source: Built-in Functions. Checked 2026-10-16.