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 Return Multiple Values From a Function
Python Keywords and Identifiers
Python Program to Find Factorial of Number Using Recursion
Python Generators
Python Program to Add Two Matrices
Python compile()
Python Program to Sort a Dictionary by Value
Python strptime()
Python String translate()
Python hash()
Python Program to Get the File Name From the File Path
Python Program to Illustrate Different Set Operations
Python Dictionary fromkeys()
Python Program to Represent enum
Python slice()
Python Set add()
Python Operator Overloading
Deep Learning in Python - LazyProgrammer
Python List
Python Package
Python Deep Learning Cookbook - Indra den Bakker
Python Function Arguments
Python List pop()
Python time Module
Python Program to Count the Number of Digits Present In a Number
Python len()
Python String isalpha()
Python Program to Split a List Into Evenly Sized Chunks
Python Namespace and Scope
Python Program to Compute the Power of a Number
Python Multiple Inheritance
Python Program to Convert String to Datetime