CKTIS / Python
vars()
Mit Argument liest es das `__dict__` des Objekts.
Version: Python 3.14; repository image, production runtime not attested
Syntax
vars()
vars(object, /)Mit Argument liest es das __dict__ des Objekts.
Die gewöhnliche Instanz liefert ihr lebendes Wörterbuch, daher setzt der Eintrag width mit 6 auch box.width auf 6. Einer ganzen Zahl fehlt dieses Wörterbuch, es folgt TypeError. Die Klasse liefert eine schreibgeschützte Ansicht.
Ohne Argument wirkt vars wie locals.
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"
Beispiele
class Box:
pass
box = Box()
vars(box)["width"] = 6
print(box.width)
try:
vars(3)
except TypeError as error:
print(type(error).__name__)
Ergebnis
6 TypeError
Ohne Argument wirkt vars wie locals.