CKTIS / Python
vars()
Con un argomento recupera il `__dict__` dell’oggetto.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
vars()
vars(object, /)Con un argomento recupera il __dict__ dell’oggetto.
L’istanza ordinaria espone il suo dizionario attivo e aggiungere width con 6 rende box.width uguale a 6. Un intero non ha tale dizionario e solleva TypeError. Una classe fornisce una vista di sola lettura.
Senza argomento vars agisce come 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"
Esempi
class Box:
pass
box = Box()
vars(box)["width"] = 6
print(box.width)
try:
vars(3)
except TypeError as error:
print(type(error).__name__)
Risultato
6 TypeError
Senza argomento vars agisce come locals.