Skip to content

Python vars() function

  • by

Python vars() function is an inbuilt function and returns the__dict__ (dictionary mapping) attribute of the given object.

vars(object)

The vars() function takes only one optional parameter. It takes an object as a parameter which may be can a module, a class, an instance, or any object having __dict__ attribute

In case the function is used without parameters A dictionary containing local symbol table is displayed.

Python vars() function

A simple example code returns the dict of a dictionary object.

class Person:
    name = "John"
    age = 25
    country = "India"


x = vars(Person)
print(x)

Output:

Python vars() function

No __dict__ Attribute Argument

string = "John"

# vars() with a string
print(vars(string))

Output:

TypeError: vars() argument must have dict attribute

Do comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading