Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python getattr() Function



ThePython getattr() function is used to access the attributes of an object. If the specified attribute is not found, it returns the default value.

Unlikesetattr() function, which is used to assign a value to the attribute of an object, thegetattr() function is used to get the value of the specified object.

Thesetattr() is one of thebuilt-in functions and you do not need to import any module to use it.

Syntax

The following is the syntax for the Pythongetattr() function.

getattr(object, attribute, default)

Parameters

The following are the parameters of the Pythongetattr() function −

  • object − This parameter specifies an object whose attribute needs to be searched.

  • attribute − This parameter represents an attribute whose value we want to get.

  • default − This parameter is optional and it specifies the value which will be returned when the specified attribute does not exist.

Return Value

The Pythongetattr() function returns the value of the named attribute of the given object. If the attribute is not found the default value will be returned.

getattr() Function Examples

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

Example: Use of getattr() Function

The following is an example of the Python getattr() function. In this, we have defined a class and instantiated its object and then, tried to retrieve the value of specified attribute.

class Car:   wheels = 4transport = Car()output = getattr(transport, "wheels") print("How many wheels does the car have:", output)

On executing the above program, the following output is generated −

How many wheels does the car have: 4

Example: Get Value of Inherited Objects Using getattr()

Using the getattr() function, we can also retrieve the value of inheritedattributes. In the code below, we have defined a parent class and its subclass. Then, using the getattr() function, we are accessing the value of attribute contained in the parent class.

class Car:   wheels = 4class Tata(Car):   fuelType = "Petrol"newCar = Tata()output = getattr(newCar, "wheels") print("The number of wheels the new car has:", output)

The following is the output obtained by executing the above program −

The number of wheels the new car has: 4

Example: Get Value of Method Using getattr()

In the following example, we are using the getattr() function to access the value of givenmethod of the specified class.

class AI:   def genAI(self):      return "This is your prompt"chatGpt = AI()output = getattr(chatGpt, "genAI")print("The chat GPT wrote:", output())

The following output is obtained by executing the above program -

The chat GPT wrote: This is your prompt

Example: Get Values of Class Attributes Using getattr()

We can also access the value of a given attribute using the getattr() function as demonstrated in the below example.

info = ["name", "emp_id", "status"]class Emp:   def __init__(self, name, emp_id, status):      self.name = name      self.emp_id = emp_id      self.status = statusemp = Emp("Ansh", 30, "Present")print("The information of Employee:")for i in info:   print(getattr(emp, i))

The above program, on executing, displays the following output -

The information of Employee:Ansh30Present
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp