Remember
vars(object) returns its __dict__ when one exists. It is not available for every object.
vars(object) returns its __dict__ when one exists. It is not available for every object.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Box:
pass
box = Box()
box.value = 3
print(vars(box))Top-level scope
class BoxObject 2 · BoxBox({'value': 3})vars(object) returns its __dict__ when one exists. It is not available for every object.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Box:
pass
box = Box()
box.value = 3
print(vars(box))
|
v
DISPLAYED RESULT
{'value': 3}
FINAL BINDINGS
Box --> object 1 (type)
box --> object 2 (Box)
object 1: class Box
object 2: Box({'value': 3})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Box:
pass
box = Box()
box.value = 3
print(vars(box))Top-level scope
class BoxObject 2 · BoxBox({'value': 3})The example has finished. Compare the final output with the values and references above.
{'value': 3}
What do you expect this program to print?
class Box:
pass
box = Box()
box.value = 3
print(vars(box))vars() FunctionThe vars() function returns the __dict__ attribute of an object. This is usually a dictionary containing all the writable attributes of the object. It’s useful when you want to inspect or work with an object's internal namespace.
vars([object])
object (optional) – The object whose __dict__ attribute you want to access. If no argument is provided, it returns the dictionary of the local symbol table.__dict__ attribute of the object or the current local symbol table if no argument is given.class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
s = Student("Alice", "A")
print(vars(s))
{'name': 'Alice', 'grade': 'A'}
Why? Because vars(s) returns the dictionary of attributes of the s object.
x = 10
y = "Hello"
print(vars())
{'x': 10, 'y': 'Hello', ...}
This shows the local symbol table as a dictionary. It's useful for debugging or introspection.
If you pass an object that doesn’t have a __dict__ attribute, Python raises a TypeError.
print(vars(42))
TypeError: vars() argument must have __dict__ attribute
Understanding vars() helps when you're dealing with object serialization, debugging tools, or custom class inspection.
vars() shows the __dict__ of an object (if present).Create a class Book with attributes title and author. Create an object and print all its attributes using vars().
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
b = Book("Python 101", "John Doe")
print(vars(b))
Connect what you noticed in the example with the idea below.
vars(object) returns its __dict__ when one exists. It is not available for every object.
vars() works on every integer, string and object.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.