Skip to main content

vars()

The vars() function returns the __dict__ attribute of the given object. This function is used to return the __dict__ attribute of the object which is a dictionary containing the object’s namespace.

Parameter Values

Parameter Description
object

Optional. The object whose __dict__ attribute will be returned. If no object is provided, it returns the __dict__ of the current local scope in the form of a dictionary.

Return Values

The vars() function returns a dict type containing the __dict__ attribute.

How to Use vars() in Python

Example 1:

The vars() function returns the __dic__ attribute of a module, class, instance, or any other object as a dictionary. It allows you to access the namespace of an object as a dictionary.

vars(list)
Example 2:

You can use the vars() function to inspect the attributes of a class instance.

class Car:
    def __init__(self, color, brand):
        self.color = color
        self.brand = brand

car = Car('red', 'Toyota')
print(vars(car))
Example 3:

The vars() function can also be used to get the attributes of a built-in module.

import math
print(vars(math))