Table of Contents
The vars() function returns the __dict__ attribute of the given object.
The syntax of the vars() function is:
vars(object)
1. vars() Parameters
vars() takes a maximum of one parameter.
- object – can be module, class, instance, or any object having the
__dict__attribute.
2. Return Value from vars()
vars()returns the__dict__attribute of the given object.- If the object passed to
vars()doesn’t have the__dict__attribute, it raises aTypeErrorexception. - If no argument is passed to
vars(), this function acts like locals() function.
Note: __dict__ is a dictionary or a mapping object. It stores object’s (writable) attributes.
3. Example: Working of Python vars()
class Foo:
def __init__(self, a = 5, b = 10):
self.a = a
self.b = b
object = Foo()
print(vars(object))
Output
{'a': 5, 'b': 10}
Also, run these statements on Python shell:
>>> vars(list)
>>> vars(str)
>>> vars(dict)
Related posts:
Python String isspace()
Python Program to Multiply Two Matrices
Python sleep()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Variables, Constants and Literals
Python input()
Python next()
Python isinstance()
Python String isalpha()
Python Inheritance
Python Program to Print Hello world!
Python bin()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python callable()
Python Program to Parse a String to a Float or Int
Python String find()
Python tuple()
Python Program to Catch Multiple Exceptions in One Line
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Remove Duplicate Element From a List
Python Set isdisjoint()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Display Powers of 2 Using Anonymous Function
Python String upper()
Python pow()
Python String isalnum()
Python iter()
Python Program to Get the File Name From the File Path
Python Dictionary fromkeys()
Python Dictionary clear()
Python str()
Python Type Conversion and Type Casting