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.
In previous tutorials, we have seen various Python built-in functions along with examples.
Table of Contents
Python vars() function
Python vars() function is a built-in function that is used to return the __dict__ attribute of the given object.
Syntax:
The syntax of vars function in Python is:-
vars(object)
Parameter:
The parameters of the vars function in Python is an object:-
- object:- Any object with a __dict__ attribute.
Return Value
The return value of vars() function in Python is dict attribute of given object.
Example
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}
Conclusion:
In this article, you have learned all about the Python vars() function to return dict attribute of the given object.
If you like this article, please share and keep visiting for further Python built-in functions tutorials.
Python built-in functions
Fore more information:- Click Here