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 Merge Mails
Python Program to Make a Flattened List from Nested List
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python open()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Get File Creation and Modification Date
Python locals()
Python String isdecimal()
Python Object Oriented Programming
Python Program to Create Pyramid Patterns
Python String format_map()
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Count the Number of Digits Present In a Number
Python @property decorator
Python Functions
Python Program to Find HCF or GCD
Python Program to Solve Quadratic Equation
Python getattr()
Python all()
Python any()
Python RegEx
Python Shallow Copy and Deep Copy
APIs in Node.js vs Python - A Comparison
Python Dictionary setdefault()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String istitle()
Python Program to Illustrate Different Set Operations
Python Set issubset()
Python Program to Count the Number of Each Vowel
Python String startswith()
Python Set pop()