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 List copy()
Python Program to Measure the Elapsed Time in Python
Python Program to Iterate Over Dictionaries Using for Loop
Python Dictionary clear()
Python tuple()
Python pass statement
Python Program to Check Armstrong Number
Python Global, Local and Nonlocal variables
Python object()
Python Program to Get File Creation and Modification Date
Python bin()
Python Dictionary copy()
Python del Statement
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Return Multiple Values From a Function
Python Program to Parse a String to a Float or Int
Python String expandtabs()
Python Program to Catch Multiple Exceptions in One Line
Python String split()
Python Program to Display Calendar
Python next()
Python Program to Display Powers of 2 Using Anonymous Function
Python String capitalize()
Python Custom Exceptions
Python List append()
Python zip()
Python Dictionary items()
Python Inheritance
Python oct()
Python Program to Print Colored Text to the Terminal
Python Program to Print all Prime Numbers in an Interval
Python bool()