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 Find the Square Root
Python Program to Find the Sum of Natural Numbers
Python time Module
Python Multiple Inheritance
Python Program to Find Hash of File
Python Objects and Classes
Python Object Oriented Programming
Python abs()
Python Program to Transpose a Matrix
Python Program to Check If Two Strings are Anagram
Python Set discard()
Node.js vs Python for Backend Development
Python frozenset()
Python String casefold()
Python String isdecimal()
Python String title()
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Create Pyramid Patterns
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Find the Size (Resolution) of a Image
Python break and continue
Python Program to Merge Mails
Python Program to Display Calendar
Python format()
Python Program to Shuffle Deck of Cards
Python Tuple
Python Program to Find the Largest Among Three Numbers
Python Set clear()
Python Program to Extract Extension From the File Name
Python Program to Convert Celsius To Fahrenheit
Python String rindex()
Python Program to Get the Full Path of the Current Working Directory