89DEVs

Python vars() function

overview of vars()

The vars() function returns the attribute dictionary of objects. It is a shorthand function, that returns the __dict__attribute of the given object.

use of vars()

The vars() function is used to return the attributes of the given object. # use of the vars() function class Dog: def __init__(self, age = 3, name = 'rex'): self.age = age self.name = name rex = Dog() print(vars(object)) The attributes of the given object are returned. {'__repr__': , '__hash__': , '__str__': , '__getattribute__': , '__setattr__': , '__delattr__': , '__lt__': , '__le__': , '__eq__': , '__ne__': , '__gt__': , '__ge__': , '__init__': , '__new__': , '__reduce_ex__': , '__reduce__': , '__subclasshook__': , '__init_subclass__': , '__format__': , '__sizeof__': , '__dir__': , '__class__': , '__doc__': 'The base class of the class hierarchy.\n\nWhen called, it accepts no arguments and returns a new featureless\ninstance that has no instance attributes and cannot be given any.\n'}

syntax of vars()

The syntax of the vars() function is: vars([object])

arguments of vars()

The vars() function accepts up to one argument, it is not required. The argument has to be an object like a module, class, instance, or any other object that has the __dict__ attribute.
argument required description
object optional the __dict__ attribute of the object is returned

return values of vars()

The vars() function returns the attributes of the given object. If no object is passed, all the variables in the global scope are returned. {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': }

                
        

Summary


Click to jump to section