Hello Python Programmers, Today, In this article, you are going to learn all about the Python vars() function to return the __dict__ attribute of the given object. Python vars() function returns __dict__ attribute for an object, a module, a class, or an object having __dict__ attribute.
Headings of Contents
Python vars introduction
Python vars() function is a built-in function which means you don’t need to install it by using the pip command because it comes with Python by default. Python vars function is used to return the __dict__ attribute of the given object, a class, an instance, or an object having __dict__ attribute available.
Syntax:
The syntax of the vars function in Python is:-
vars(object)
Parameter:
The parameter of the vars function in Python is an object:-
- object:- Any object with a __dict__ attribute.
Return Value
The return value of the vars() function in Python is the __dict__ attribute of a given object.
Note:- The vars() function without argument will return all a dictionary containing the attributes of the local symbol table.
Example: Using vars() function with an object having __dict__ attribute
class Calculation:
def __init__(self, a, b):
self.x = a
self.y = b
def Addition(self):
print(f"The Addition of {self.x} and {self.y} is:- {self.x + self.y}")
c1 = Calculation(12, 23)
c1.Addition()
print("-------------------")
x = vars(Calculation)
print(x)
Output:
The addition of 12 and 23 is:- 35
-------------------
{'__module__': '__main__', '__init__': <function Calculation.__init__ at 0x000001F6E829D280>, 'Addition': <function Calculation.Addition at 0x000001F6E829D1F0>, '__dict__': <attribute '__dict__' of 'Calculation' objects>, '__weakref__': <attribute '__weakref__' of 'Calculation' objects>, '__doc__': None}
Example: Using vars() function without argument
The vars() function without any argument will return all the attributes and methods of local scope.
from datetime import datetime
from time import sleep
result = vars()
print(result)
Output
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000207DF324790>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\Vishvajit Rao\\OneDrive\\Desktop\\Pyspark\\main.py', '__cached__': None, 'datetime': <class 'datetime.datetime'>, 'sleep': <built-in function sleep>, 'result': {...}}
Example: Using vars() function with no __dict__ attribute argument
When you call the vars() function for an object haven’t any __dict__ attribute, in That situation, it will raise a TypeError exception.
lst = ["Vishvajit", "Rao", "Harshita", "Kumari"]
print(vars(lst))
Output
TypeError: vars() argument must have __dict__ attribute
Conclusion:
In this guide, you have seen all about the Python vars() function to return __dict__ attribute of the given object. vars function in Python takes an object as a parameter and returns the attribute of that object as a dictionary. If you like this article, please share and keep visiting for further Python built-in functions tutorials.
Python built-in functions
For more information:- Click Here
Thanks for reading ……… 🙏🙏❤️❤️