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 Program to Sort Words in Alphabetic Order
Python List index()
Python Tuple count()
Python pass statement
Python locals()
Python Program to Capitalize the First Character of a String
Python Dictionary clear()
Python Deep Learning Cookbook - Indra den Bakker
Python Dictionary fromkeys()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String isidentifier()
Python Program to Reverse a Number
Python Program to Find HCF or GCD
Python Namespace and Scope
Python Program to Find Numbers Divisible by Another Number
Python float()
Python Program to Convert Bytes to a String
Python Program to Get the Class Name of an Instance
Python String split()
Python Program to Make a Simple Calculator
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Set add()
Python del Statement
Python sleep()
Python String count()
Python Tuple
Python Set union()
Python String center()
Python property()
Python Program to Catch Multiple Exceptions in One Line
Python slice()
Python Program to Find Hash of File