CKTIS / Python
vars()
Returns the attribute dictionary of an object that provides one.
Version: Python 3.14; repository image, production runtime not attested
Syntax
vars()
vars(object, /)Returns the attribute dictionary of an object that provides one.
Mutating the returned dictionary can change ordinary instance attributes. Objects using only slots may have no such dictionary.
With an argument, retrieves its __dict__. The ordinary instance exposes the live dictionary, so adding width there makes box.width equal to 6. An integer has no such dictionary and raises TypeError. A class exposes a read-only mapping proxy. With no argument, vars behaves like locals in that scope.
class Box: pass
b = Box()
d = vars(b)
d["width"] = 6
assert d is b.__dict__ and b.width == 6
assert type(vars(Box)).__name__ == "mappingproxy"
Examples
class Box:
pass
box = Box()
vars(box)["width"] = 6
print(box.width)
try:
vars(3)
except TypeError as error:
print(type(error).__name__)
Result
6 TypeError
With an argument, retrieves its `__dict__`. The ordinary instance exposes the live dictionary, so adding width there makes box.width equal to 6. An integer has no such dictionary and raises TypeError. A class exposes a read-only mapping proxy. With no argument, vars behaves like locals in that scope.