English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Fonctions en Python

Types de données en Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python vars() usage and examples

Built-in functions in Python

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

The syntax of the vars() function is:

vars(object)

vars() parameter

vars() accepts at most one parameter.

  • object-can be a module, class, instance, or any object that has a __dict__ attribute.

vars() return value

  • vars() returns the properties of the __dict__ given object.

  • If the object passed to vars() does not have a __dict__ attribute, it will raise a TypeError exception.

  • If no parameters are passed to vars(), the function acts likelocals() function.

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

Example: How Python vars() works

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

Output result

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

In addition, run the following statements in the Python shell:

>>> vars(list)
>>> vars(str)
>>> vars(dict)

Built-in functions in Python