Python Reference Library →

Python vars() Function – View Object’s Attributes

Let’s look at vars() function – view object’s attributes

  1. Notice

    vars(object) returns its __dict__ when one exists. It is not available for every object.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Box:
    pass
box = Box()
box.value = 3
print(vars(box))
Live state

Top-level scope

Box→Object 1
box→Object 2
Object 1 · typeclass BoxObject 2 · BoxBox({'value': 3})
This step

vars(object) returns its __dict__ when one exists. It is not available for every object.

Picture vars() function – view object’s attributes

vars(object) returns its __dict__ when one exists. It is not available for every object.

Image brief

Alt text
A diagram for Python vars() Function – View Object’s Attributes. Instructions: class Box: pass box = Box() box.value = 3 print(vars(box)). Displayed result: {'value': 3}. The final example bindings are Box --> object 1 (type); box --> object 2 (Box). object 1: class Box; object 2: Box({'value': 3})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python vars() Function – View Object’s Attributes". Teaching focus: vars(object) returns its __dict__ when one exists. It is not available for every object. Depict exactly the relationships and labels in this reference diagram: 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}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python vars() Function – View Object’s Attributes. Instructions: class Box: pass box = Box() box.value = 3 print(vars(box)). Displayed result: {'value': 3}. The final example bindings are Box --> object 1 (type); box --> object 2 (Box). object 1: class Box; object 2: Box({'value': 3})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Box:
    pass
box = Box()
box.value = 3
print(vars(box))
Live state

Top-level scope

Box→Object 1
box→Object 2
Object 1 · typeclass BoxObject 2 · BoxBox({'value': 3})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
{'value': 3}

What do you expect this program to print?

What does this example display?

class Box:
    pass
box = Box()
box.value = 3
print(vars(box))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

{'value': 3}

Follow the printed values in order. The output is: {'value': 3}

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

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.

More examples and reference

Python vars() Function

The 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.

Syntax

vars([object])

Parameters:

  • 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.

Returns:

  • A dictionary representing the __dict__ attribute of the object or the current local symbol table if no argument is given.

Example 1: Using vars() on a Class Instance

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.

Example 2: Using vars() Without Arguments

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.

Use Case: When to Use vars()?

  • To inspect the attributes of an object (e.g., class instances)
  • For debugging and exploring local/global scope contents
  • When dynamically working with attributes and values

What if the Object Doesn’t Have __dict__?

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

Interview Tip

Understanding vars() helps when you're dealing with object serialization, debugging tools, or custom class inspection.

Summary

  • vars() shows the __dict__ of an object (if present).
  • Without arguments, it shows the local symbol table.
  • Useful for introspection, debugging, and meta-programming.

Practice Problem

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))

What to remember

Connect what you noticed in the example with the idea below.

Remember

vars(object) returns its __dict__ when one exists. It is not available for every object.

Watch out for this idea

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.

    Reference library →