Python Glossary Русский

all Python 3Built-in functionsterm

vars

introspection

Returns the __dict__ of an object, or a dictionary of the current local namespace (with no argument — the same as locals()).

Syntax

vars([object])

vars(instance) shows only the instance's own attributes — class attributes and methods are not there, they live in vars(type). What you get is the live __dict__ rather than a copy, so mutating it genuinely changes the object. And a class declaring __slots__ (or a built-in type such as int) has no __dict__ at all, so vars raises TypeError.

Examples

class A:
    def __init__(self): self.x=1; self.y=2
a=A()
print(vars(a))  # → {'x':1,'y':2}
print('x' in vars(a))  # → True
print(vars(int)['__add__'])  # → ? слот-обёртка int.__add__ (repr содержит адрес)
print(type(vars()))  # → dict
print('print' in vars(__builtins__) if isinstance(vars(__builtins__),dict) else True)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy