Python vars()
The vars() is a built-in python function that returns the \_\_dict\_\_ attribute of an object.
Python vars()
The syntax of the vars() function is :
1
2
vars(object)
vars() parameters
vars() function takes only one parameter as argument:
- object - Any object having the __dict__ attribute or module,class ,instance.
Example 1: How to use vars() function on python?
1
2
3
4
5
6
7
8
class Dog:
def __init__(self, a = 4, b = 8):
self.a = a
self.b = b
object = Dog()
print(vars(object))
Output:
1
2
{'a': 4, 'b': 8}
The __dict__ attribute is a dictionary containing the specific object’s changeable attributes.
Rules of vars()
- If called without any parameters, it will return a dictionary containing the local symbol table.
- vars() will return the __dict__ attributes of the given object.
- If the object passed to vars() doesn’t have the __dict__ attribute, it raises a TypeError exception.
This post is licensed under CC BY 4.0 by the author.