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
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)
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))
The vars()
function can also be used to get the attributes of a built-in module.
import math
print(vars(math))