The vars()
function returns the __dict__
attribute of an object such as a class, a module e.t.c. The __dict__
attribute stores the namespace dictionary associated with an object.
When you access an attribute using dot notation (e.g., object.attribute
), Python internally looks up the attribute in the object's __dict__
dictionary. If the attribute is found in the dictionary, its value is returned. Otherwise, the search in the object's class and then in the parent classes.
Namespaces such as modules, classes, instances, and functions have their own __dict__
attribute, which serves as their namespace dictionary. This dictionary is where the names and their associated objects are stored.
If the vars() function is called with an object, it returns the __dict__ dictionary . If it is called without any argument, the current local scope is used as the default.
Syntax:
vars(object[optional])
copy
If the optional argument, namespace
, is not given, the function returns all the variables in the current local scope.
The function is usually used in debugging scenarios to inspect the values of variables at different points in the code. Additionally, you can modify the variables indirectly through the returned dictionary because the values in the dictionary are references to the original variables.
However, it's important to exercise caution when modifying variables indirectly as it can lead to unexpected behavior and make the code more difficult to understand and maintain.