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 aTypeErrorexception. - 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 Represent enum
Python Set issubset()
Python input()
Python Program to Capitalize the First Character of a String
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String expandtabs()
Python Dictionary pop()
Python Global Keyword
Python Program to Display the multiplication Table
Python break and continue
Python Program to Convert Celsius To Fahrenheit
Python Program to Create Pyramid Patterns
Python Set copy()
Python Program to Calculate the Area of a Triangle
Python callable()
Python Program to Make a Simple Calculator
Python Program to Remove Punctuations From a String
Python Program to Count the Occurrence of an Item in a List
Python hex()
Python object()
Python sum()
Python property()
Python Program to Get the File Name From the File Path
Python Keywords and Identifiers
Python format()
Python delattr()
Python Program to Check If a List is Empty
Python String isprintable()
Python round()
Python Program to Merge Mails
Python locals()
Python String isupper()