Python vars()

Filed Under: Python

Python vars() function returns __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. So the output of vars() function is a dictionary.

Python vars()

Python vars() function syntax is:


vars([object])
  • If no argument is provided, vars() act like locals() function.
  • The arguments can be module, class or instance of a class.
  • If the specified argument doesn’t have __dict__ attribute, the TypeError exception is thrown with message TypeError: vars() argument must have __dict__ attribute.
  • If we update the object __dict__ dictionary values, then the updated value will be returned by vars() function.

vars() of class and object

Let’s say we have a class defined with some class variable and instance variables.


class Data:
    # class variables
    id = 0
    name = ''

    def __init__(self, i, n):
        self.id = i
        self.name = n
        # instance variable
        self.repr = 'Data[%s,%s]' % (i,n)

Let’s see the vars() function output when an instance of the class is provided.


d = Data(1, 'Pankaj')

# vars of object
print(vars(d))

# update __dict__ and then call vars()
d.__dict__['id'] = 100
print(vars(d))

Output:


{'id': 1, 'name': 'Pankaj', 'repr': 'Data[1,Pankaj]'}
{'id': 100, 'name': 'Pankaj', 'repr': 'Data[1,Pankaj]'}

Let’s see the vars() function output with the class as an argument.


print(vars(Data))

Output:


{'__module__': '__main__', 'id': 0, 'name': '', '__init__': <function Data.__init__ at 0x108566158>, '__dict__': <attribute '__dict__' of 'Data' objects>, '__weakref__': <attribute '__weakref__' of 'Data' objects>, '__doc__': None}

vars() with module


import math

print(vars(math))

Output:


{'__name__': 'math', '__doc__': 'This module is always available.  It provides access to the\nmathematical functions defined by the C standard.', '__package__': '', '__loader__': <_frozen_importlib_external.ExtensionFileLoader object at 0x1085654a8>, '__spec__': ModuleSpec(name='math', loader=<_frozen_importlib_external.ExtensionFileLoader object at 0x1085654a8>, origin='/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'), 'acos': <built-in function acos>, 'acosh': <built-in function acosh>, 'asin': <built-in function asin>, 'asinh': <built-in function asinh>, 'atan': <built-in function atan>, 'atan2': <built-in function atan2>, 'atanh': <built-in function atanh>, 'ceil': <built-in function ceil>, 'copysign': <built-in function copysign>, 'cos': <built-in function cos>, 'cosh': <built-in function cosh>, 'degrees': <built-in function degrees>, 'erf': <built-in function erf>, 'erfc': <built-in function erfc>, 'exp': <built-in function exp>, 'expm1': <built-in function expm1>, 'fabs': <built-in function fabs>, 'factorial': <built-in function factorial>, 'floor': <built-in function floor>, 'fmod': <built-in function fmod>, 'frexp': <built-in function frexp>, 'fsum': <built-in function fsum>, 'gamma': <built-in function gamma>, 'gcd': <built-in function gcd>, 'hypot': <built-in function hypot>, 'isclose': <built-in function isclose>, 'isfinite': <built-in function isfinite>, 'isinf': <built-in function isinf>, 'isnan': <built-in function isnan>, 'ldexp': <built-in function ldexp>, 'lgamma': <built-in function lgamma>, 'log': <built-in function log>, 'log1p': <built-in function log1p>, 'log10': <built-in function log10>, 'log2': <built-in function log2>, 'modf': <built-in function modf>, 'pow': <built-in function pow>, 'radians': <built-in function radians>, 'remainder': <built-in function remainder>, 'sin': <built-in function sin>, 'sinh': <built-in function sinh>, 'sqrt': <built-in function sqrt>, 'tan': <built-in function tan>, 'tanh': <built-in function tanh>, 'trunc': <built-in function trunc>, 'pi': 3.141592653589793, 'e': 2.718281828459045, 'tau': 6.283185307179586, 'inf': inf, 'nan': nan, '__file__': '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'}

vars() without argument


print(vars())

Output:


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x108508390>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_vars_function.py', '__cached__': None, 'Data': <class '__main__.Data'>, 'd': <__main__.Data object at 0x108565048>, 'math': <module 'math' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'>}
You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages