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 Set issuperset()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Trim Whitespace From a String
Python set()
Python Program to Count the Number of Digits Present In a Number
Python print()
Python @property decorator
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Find Factorial of Number Using Recursion
Python Dictionary get()
Python __import__()
Python timestamp to datetime and vice-versa
Python dict()
Python Dictionary keys()
Python Program to Check if a Key is Already Present in a Dictionary
Python String swapcase()
Python Global, Local and Nonlocal variables
Python strptime()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Deep Learning Cookbook - Indra den Bakker
Python ascii()
Python Program to Reverse a Number
Python property()
Python String split()
Python Closures
Python Program to Find LCM
Python String rpartition()
Python classmethod()
Python String lower()
Python Program to Differentiate Between type() and isinstance()
Python sleep()
Python String ljust()