Python getattr() Function



The python getattr() function is a built-in function used to access the attributes of an object. If the specified attribute is not found, this function will return default value.

Unlike setattr() function, which is used to assign a value to the attribute of an object, the getattr() function is used to get the value of the specified object.

In the next few sections, we will be learning more about this function.

Syntax

The following is the syntax for the Python getattr() function.

getattr(object, attribute, default)

Parameters

The following are the parameters of the Python getattr() 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 Python getattr() function returns the value of the named attribute of the given object. If the attribute is not found the default value will be returned.

Examples

In this section, we will see some examples of getattr() function −

Example 1

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 = 4

transport = 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 2

Using the getattr() function, we can also retrieve the value of inherited attributes. 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 = 4

class 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 3

In the following example, we are using the getattr() function to access the value of given method 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 4

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 = status

emp = 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:
Ansh
30
Present
python_built_in_functions.htm
Advertisements