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 __import__()
Python eval()
Python Program to Delete an Element From a Dictionary
Python bin()
Python Program to Copy a File
Python Set add()
Python String count()
Python String lower()
Python Program to Convert Decimal to Binary Using Recursion
Python List
Python String splitlines()
Python Program to Get File Creation and Modification Date
Python Operators
Python round()
Python Program to Check the File Size
Node.js vs Python for Backend Development
Python Program to Reverse a Number
Python String endswith()
How to get current date and time in Python?
Python Program to Print the Fibonacci sequence
Python id()
Python Program to Trim Whitespace From a String
Python Program to Display Calendar
Python Program to Find Numbers Divisible by Another Number
Python Dictionary clear()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Dictionary copy()
Python Program to Check if a Number is Positive, Negative or 0
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Concatenate Two Lists
Python Set remove()
Python String join()