Hello, programmers; in today’s article, we will discuss all you need to know about the vars() function in python. Python vars() is a function that returns the __dict__ attribute of an object. Before we go into the topic’s depth, let me just brief you on what are __dict__ and object in the vars() function.
Python vars() function returns the object in __dict__ form. This conversion is based on the presence of __dict__ function in an object. If this function is absent in an object, then TypeError is thrown in the vars() function. Given these points, Vars is a function that returns a dictionary out of an object if possible.
__dict__ attribute – refers to a dictionary containing the object’s mutable attributes.
Object – can be a module, class, instance or any other object possessing the __dict__ attribute.
vars() syntax – vars(object)
vars() parameters – vars() function takes only one parameter. It takes the object as its parameter to return its __dict__ attributes.
vars() return value – The vars() function originally returns the __dict__ attribute of the object passed to its as an argument. But if the object passed to the vars() function does not have any __dict__ attribute; a type error exception is raised. Thereupon, if no argument is passed to the vars() function, it behaves as a locals() function. In either case, the locals() function modifies and returns a dictionary containing the present local symbol table.
Lets see how the python vars() function works with:
- Class as object
- Instance of class as object
- Module as object
- No argument
Python vars() function with class as object:
Example:
class Individual:
name = "David"
age = 25
state = "Chicago"
print(vars(Individual))
Output:
{'name': 'David', 'age': 25, 'state': 'Chicago'}
Explanation:
In this example, a class named Individual with a name, age, and the state as its dictionary attributes. Whenever a vars() function have an Individual class as an argument, it prints the dictionary attributes of the class as output.
Instance of class as object to python vars() function:
Example:
class Data:
# class variables
id = 0
name = ''
def __init__(self, a, b):
self.id = a
self.name = b
# instance variable
self.repr = 'Data[%s,%s]' % (a,b)
d = Data(1, 'Gunjan')
# vars of object
print(vars(d))
# update __dict__ and then call vars()
d.__dict__['id'] = 50
print(vars(d))
Output:
{'id': 1, 'name': 'Gunjan', 'repr': 'Data[1,Pankaj]'}
{'id': 50, 'name': 'Gunjan', 'repr': 'Data[1,Pankaj]'}
Explanation:
In this example, a class named data is created, having class variables as ‘id’ and ‘name.’ The instance variable of the class data is also formed, having instance variables a and b.
As shown above, the math module is passed as an argument to the vars() function—the output prints all the math module’s built-in functions as its dictionary attributes. Finally, an instance of the class is passed as an argument to the vars() function.
Python vars() function with module as object:
Example:
import math
print(vars(math))
Output:
{'__name__': 'math', '__doc__': 'This module is always available. It provides access to the\nmathematical functions defined by the C standard.', '__package__': '', '__loader__'}
Explanation:
As shown above, the math module is passed as an argument to the vars() function—the output prints all the math module’s built-in functions as its dictionary attributes.
No argument as object to python vars() function:
Example:
class Data:
# class variables
id = 0
name = ''
print(vars())
Output:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__'}
Explanation:
In this example, no argument is passed to vars() function, and thus, when printed, returns a type error exception.
Must Read
- Introduction to Python Super With Examples
- Python Help Function
- Why is Python sys.exit better than other exit functions?
- Python Bitstring: Classes and Other Examples | Module
Conclusion:
In this article, we learned to implement vars function in different cases in Python. The above algorithm and code snippets with the vars function can be used whenever needed.
Still have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!