Kodeclik Blog


Python vars()

Python vars() is a function that is used to inspect objects and understand the dictionary behind the object. The dictionary behind an object is stored in a variable called “__dict__”. It stores the attributes of the object along with their values.

Python vars() with an object

Let us create a Python class called Book denoting objects with attributes called name, author, year (of publication), and publisher.
The above class (Book) has a constructor that creates an object with the given arguments, i.e., name of the book, author of the book, year of publication, and the publisher.
We can create objects of the above class with statements like this:
This statement creates a very specific book with the values given in the argument list.
The vars() function is something you can use with an object (in this case, book1) and inspect the inner workings of the object. In our case, these specific values are often stored in a dictionary and the vars() function outputs the dictionary. Thus, if we do:
The output is:
As you can see, the keys of the dictionary are ‘name’, ‘author’, ‘year’, and ‘pub’. The values are ‘Harry Potter and the Philosopher’s Stone’, ‘J.K. Rowling’, 1997, and ‘Bloomsbury’. This dictionary is stored in a pre-defined variable called “__dict__”, so you could have printed the dictionary using:
The output is again:
The vars() function is useful for inspecting objects to see what attributes and values they have. For instance you can use this to dynamically generate HTML or other text based on the attributes and values of an object.

Python vars() with a class

We can apply the vars() function to a class rather than an instance of a class:
The output is:
Note that the vars() function outputs a dictionary (but most of this output appears gibberish because this is a dictionary used internally by Python to store the details of the Book object.) The main takeaway here is that vars() can be applied to both objects and classes.

Python vars() with a dictionary

Let us create an actual dictionary that stores months and the number of days in each.
And now let us try vars() with this dictionary:
The output is:
This is very interesting! So an actual dictionary does not have a “__dict__” attribute! Why is this? Let us replace that line with:
The output is:
So, true enough, this object does not have a “__dict__” attribute. This is because Python does not quite store the details of this object in an internal __dict__ structure and thus there is no internal __dict__ object to return.

Python vars() with a list

Let us try vars() with a Python list.
As expected, the output is:

Python vars() with an integer

If we try vars() with an integer:
The output will be:
So in short, vars() is a function that takes a single argument, which can be an object, class, or module. The returned dictionary contains the object's variables and their values.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.