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 Program to Print all Prime Numbers in an Interval
Python String swapcase()
Python strptime()
Python String center()
Python Decorators
Python Program to Find the Largest Among Three Numbers
Python chr()
Python Function Arguments
Python getattr()
How to get current date and time in Python?
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program Read a File Line by Line Into a List
Python bytearray()
Python String isupper()
Python Set issubset()
Python String format_map()
Python Dictionary copy()
Python Dictionary
Python Program to Print Colored Text to the Terminal
Python Inheritance
Python Program to Add Two Numbers
Python List extend()
Python String encode()
Python Modules
Python sum()
Python Program to Find the Size (Resolution) of a Image
Python float()
Python Program to Remove Duplicate Element From a List
Python Data Structures and Algorithms - Benjamin Baka
Python zip()
Python Program to Represent enum