vars()
The built-in vars() function returns the .__dict__ attribute for a module, class, instance, or any other object with this attribute. This attribute is a dictionary-like mapping that represents the object’s namespace, pairing attribute names with their values:
>>> class Point:
... def __init__(self, x, y):
... self.x = x
... self.y = y
...
>>> point = Point(10, 20)
>>> vars(point)
{'x': 10, 'y': 20}
vars() Signatures
vars()
vars(object, /)
Arguments
| Argument | Description |
|---|---|
object |
An object with a .__dict__ attribute |
Return Value
- When an
objectwith a.__dict__attribute is provided,vars()returns a mapping representing the object’s namespace. Modules and instances have an updateable.__dict__, but other objects may restrict writes to it. A class, for example, returns a read-onlytypes.MappingProxyTyperather than a plain dictionary. - Without an argument, it returns a dictionary of the local namespace similar to
locals(). Since Python 3.13 (PEP 667), callingvars()inside a function, generator, or coroutine returns a fresh snapshot dictionary, so writing to that dictionary no longer affects the actual local variables. - If the
objectdoesn’t have a.__dict__attribute, such as when its class defines__slots__, thenvars()raises aTypeError.
vars() Examples
With a class instance as an argument:
>>> class Rectangle:
... def __init__(self, length, width):
... self.length = length
... self.width = width
...
>>> rect = Rectangle(3, 4)
>>> vars(rect)
{'length': 3, 'width': 4}
With a module object:
>>> import sys
>>> vars(sys)
{'__name__': 'sys', ..., 'ps1': '>>> ', 'ps2': '... '}
Without any argument, acting like the locals() function:
>>> a = 5
>>> b = 10
>>> vars()
{'__name__': '__main__', ..., 'a': 5, 'b': 10}
With an object whose class defines __slots__, so it has no .__dict__ attribute:
>>> class Vector:
... __slots__ = ("x", "y")
... def __init__(self, x, y):
... self.x = x
... self.y = y
...
>>> vars(Vector(1, 2))
Traceback (most recent call last):
...
TypeError: vars() argument must have __dict__ attribute
vars() Common Use Cases
The most common use cases for the vars() function include:
- Inspecting the attributes of an object dynamically.
- Debugging by examining the current namespace.
- Interacting with module-level or class-level namespaces.
vars() Real-World Example
Suppose you have a configuration class that holds various settings for an application. You can use vars() to convert the settings into a dictionary for serialization or logging:
>>> class Config:
... def __init__(self, debug=False, version='1.0'):
... self.debug = debug
... self.version = version
...
>>> config = Config(debug=True)
>>> config_dict = vars(config)
>>> config_dict
{'debug': True, 'version': '1.0'}
In this example, vars() helps convert an object’s attributes into a dictionary, which can then be serialized to JSON or logged for debugging purposes.
Related Resources
Tutorial
Namespaces in Python
In this tutorial, you'll learn about Python namespaces, the structures that store and organize the symbolic names during the execution of a Python program. You'll learn when namespaces are created, how they're implemented, and how they support variable scope.
For additional information on related topics, take a look at the following resources:
- Python Scope and the LEGB Rule: Resolving Names in Your Code (Tutorial)
- Using Python's .__dict__ to Work With Attributes (Tutorial)
- Working With Python's .__dict__ Attribute (Course)
- Navigating Namespaces and Scope in Python (Course)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Namespaces and Scope in Python (Quiz)
- Namespaces in Python (Quiz)
- The LEGB Rule & Understanding Python Scope (Course)
- Using Python's .__dict__ to Work With Attributes (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)