Python vars()

The vars() function returns the __dict__ attribute of the given object.

The syntax of the vars() function is:


vars() Parameters

vars() takes a maximum of one parameter.

  • object - can be module, class, instance, or any object having the __dict__ attribute.

Return Value from vars()

  • vars() returns the __dict__ attribute of the given object.
  • If the object passed to vars() doesn't have the __dict__ attribute, it raises a TypeError exception.
  • If no argument is passed to vars(), this function acts like locals() function.

Note: __dict__ is a dictionary or a mapping object. It stores object's (writable) attributes.

Example: Working of Python vars()

class Foo:
  def __init__(self, a = 5, b = 10):
    self.a = a
    self.b = b
object = Foo()


{'a': 5, 'b': 10}

Also, run these statements on Python shell:

>>> vars(list)
>>> vars(str)
>>> vars(dict)
Did you find this article helpful?