As Python stores their instance variables in the form of dictionary belonging to the respective object both
vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.
This function displays more attributes than vars() function, as it is not limited to an instance. It displays the class attributes as well. It also displays the attributes of its ancestor classes.
This function displays the attribute of an instance in the form of a dictionary.
The below table provides with some significant difference between var() and dir():
|Returns a dictionary of objects of single class where used||Returns a dictionary of objects of single class where used and its base classes|
|It returns a dictionary corresponding to the current local symbol table when no argument is passed||It returns the list of names in the current local scope when passed no argument|
|It returns a dictionary corresponding to the object’s symbol table if a module, class or class instance object as argument (or anything else that has a __dict__ attribute) is passed.||It attempt to return a list of valid attributes for that object when passed an argument|
|As instances builtin types do not have __dict__ attribute, it returns an Error when used in a buil-in type instance.||It can be used with all built-in types without error|
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- vars() function in Python
- dir() function in Python
- Difference between 'and' and '&' in Python
- Python set operations (union, intersection, difference and symmetric difference)
- Difference between Method and Function in Python
- Python | Difference between iterable and iterator
- Difference between List and Array in Python
- Python | Difference between Pandas.copy() and copying through variables
- Difference between List comprehension and Lambda in Python
- Difference between Python and C#
- Difference between C and Python
- Python | Difference Between List and Tuple
- Difference Between Go and Python Programming Language
- Difference between continue and pass statements in Python
- Difference between input() and raw_input() functions in Python
- Difference between dict.items() and dict.iteritems() in Python
- Python - Difference between sorted() and sort()
- Difference between List and Dictionary in Python
- Python: Difference between Lock and Rlock objects
- Python program to find difference between current time and given time
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.