Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python vars() Function



ThePython vars() function is abuilt-in function that returns the__dict__ attribute of the associated object. This attribute is a dictionary containing all the mutable attributes of the object. We can also say this function is a way of accessing the attributes of the object in a dictionary format.

If we call thevars() function without any argument, it acts similarly to thelocals() function and will return a dictionary containing the local symbol table.

Always remember, each Python program has a symbol table that contains information about the names (variables,functions, classes, etc.) defined in the program.

Syntax

The syntax of the Pythonvars() function is as follows −

vars(object)

Parameters

The Pythonvars() function accepts a single parameter −

  • object − This parameter represents an object with __dict__ attribute. It could be a module, a class, or an instance.

Return Value

The Pythonvars() function returns the __dict__ attribute of the specified size. If no arguments are passed, it will return the local symbol table. And, if the passed object does not support __dict__ attribute, it raises TypeError exception.

vars() Function Examples

Practice the following examples to understand the use ofvars() function in Python:

Example: Use of vars() Function

On applying thevars() function on a user-defined class, it returns theattributes of that class. In the following example, we have defined a class and a method with three attributes. And, we are displaying them usingvars() function.

class Vehicle:   def __init__(self, types, company, model):      self.types = types      self.company = company      self.model = model        vehicles = Vehicle("Car", "Tata", "Safari")print("The attributes of the Vehicle class: ")print(vars(vehicles))

When we run above program, it produces the following result −

The attributes of the Vehicle class: {'types': 'Car', 'company': 'Tata', 'model': 'Safari'}

Example: Use of vars() Function With Built-in Module

If we use thevars() function with abuilt-in module, it will display the description of that module. In the code below, we are importing the string method and with the help ofvars(), we list the detailed description of this module.

import stringattr = vars(string)print("The attributes of the string module: ", attr)

Following is an output of the above code −

The attributes of the string module:  {'__name__': 'string', '__doc__': 'A collection of string constants......}

Example: Get Attributes of User-defined Function Using vars()

In the following example, we have created auser-defined method named "newFun", and tried to display its attributes usingvars() function.

def newFun():   val1 = 10   val2 = 20   print(vars())print("The attributes of the defined function:")newFun()

Output of the above code is as follows −

The attributes of the defined function:{'val1': 10, 'val2': 20}
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp