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 String isupper()
Python Program to Display Powers of 2 Using Anonymous Function
Python list()
Python List Comprehension
Python Program to Illustrate Different Set Operations
Python Function Arguments
Python List append()
Python Program to Sort a Dictionary by Value
Python Program to Reverse a Number
Python Input, Output and Import
Python locals()
Python format()
Python Program to Create a Long Multiline String
Python hex()
Python String casefold()
Python Program to Check if a Number is Odd or Even
Python Program to Concatenate Two Lists
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Add Two Matrices
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python List count()
Python Global Keyword
Python Set intersection()
Python Get Current time
Python Program to Find Factorial of Number Using Recursion
Python Program to Count the Occurrence of an Item in a List
Python Program to Multiply Two Matrices
Python Global, Local and Nonlocal variables
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Display Calendar
Python set()