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 Count the Number of Digits Present In a Number
Python Global Keyword
Python Set symmetric_difference()
Python Program to Trim Whitespace From a String
Python memoryview()
Python Inheritance
Python Multiple Inheritance
Python open()
Python Dictionary setdefault()
Python Program to Find HCF or GCD
Python String ljust()
Python Tuple count()
Python Program to Get the Class Name of an Instance
Python Program to Reverse a Number
Python __import__()
Python while Loop
Python break and continue
Python time Module
Python set()
Python max()
Python Sets
Python Function Arguments
Intelligent Projects Using Python - Santanu Pattanayak
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Get the File Name From the File Path
Python String isupper()
Python String rfind()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String isdigit()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python List Comprehension
Python Program to Check If a String Is a Number (Float)