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 ord()
Python Program to Make a Flattened List from Nested List
Python Program to Display Calendar
Python super()
Python Program to Extract Extension From the File Name
Python Program to Iterate Through Two Lists in Parallel
Python String zfill()
Python callable()
Python Package
Python if...else Statement
Python Iterators
Python globals()
Python round()
Python zip()
Python List sort()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Make a Simple Calculator
Python Program to Access Index of a List Using for Loop
Python Program to Check Armstrong Number
Python Program to Count the Number of Each Vowel
Python Program to Delete an Element From a Dictionary
Python Program to Generate a Random Number
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Remove Punctuations From a String
Python String isdecimal()
Python bytearray()
Python chr()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python abs()
Python Program to Add Two Numbers
Python Dictionary update()