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 aTypeError
exception. - 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 if...else Statement
Python Tuple
Python Program to Find Numbers Divisible by Another Number
Python Program to Append to a File
Python bytes()
Python String ljust()
Python Statement, Indentation and Comments
Python Dictionary clear()
Python min()
Python Set copy()
Python String split()
Python Set intersection_update()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Object Oriented Programming
Python dict()
Python Program to Split a List Into Evenly Sized Chunks
Python Dictionary items()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Get Line Count of a File
Python eval()
Python File I/O Operation
Python Program to Remove Punctuations From a String
Python reversed()
Python enumerate()
Python Global, Local and Nonlocal variables
Python Program to Check if a Number is Odd or Even
Python del Statement
Python Program to Reverse a Number
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Get the Class Name of an Instance
Python Program to Create a Countdown Timer
Python List pop()