vars()

vars(object)

Returns the dictionary from the __dict__ attribute of the specified object.

Parameters

  • obj: Object for which the attribute dictionary (__dict__) must be returned.

Note: The __dict__ attribute of modules and instances supports modification, but other objects may restrict its modification. For example, classes use a proxy to prevent direct modification of this dictionary.

If vars is called without arguments, it behaves like locals().

Return Value

The vars() function returns:

  • The dictionary of attributes (__dict__) of the specified object—module, class, instance, or any other object with a __dict__ attribute.
  • Methods in the local scope when no arguments are passed.
  • TypeError if the passed object does not have a __dict__ attribute.

Examples

string = "John"

# vars() with a string
print(vars(string))

# Result
# TypeError: vars() argument must have __dict__ attribute