Home Python What is getattr() Function in Python? What is Getattr () used for

What is getattr() Function in Python? What is Getattr () used for

0

In this article we will discuss the getattr() function that is used in the python programming language, getattr() function is basically used in any type of programming logics in python IDE, this function actually returns the default value if this function finds no attribute in the program.

So in order to get the value of an object’s attribute from the getattr function, we have to call this function and if the function finds no attribute then it will return the default function values.

And the reason why we call this function in our program just because if we are missing some attributes from our program then this function will help us out to get the default value from the function.

So before any further more discussion about this topic, let’s take a look on the syntax of this function,

getattr(object_value, attribute_value[, default_value])

In this syntax above there are three things that we need to discuss this function, getattr() is basically a function name, we call it like this” getattr()” in python and inside these “()” parenthesis there are three parameters which are object value, attribute name, and default value.

getattr() Function

Let’s cover up a small example of this program, which is written below in the image as well.

Example code

in this example of code, I will explain how can we call the attribute values of an object, using getattr() function, Let’s take an example of a patient with his hospital ID number, in this example we are taking a class with the name of the patient, And there are two attributes in the patient class, Patient ID and Patient Name which is admitted in the hospital.

After making the class, we will make the object of the patient_id and patient_name and after that, we are going to call this attribute with its class.

class Patient:

Patient_id=””

Patient_name=””

# Here we call constructor to set the values of id

def __init__(self):

self.Patient_id = “154601”

self.Patient_name = “Ammar”

Patient = Patient()

# in this portion of this code we are getting the attribute values by using getattr() function

print(‘\ngetattr : name of the Patient is =’, getattr(Patient, “Patient_name”))

# but you could access this like this

print(‘traditional: name of the Patient is =’, Patient.Patient_name)

What is getattr() Function in Python

So the output of this program should be like this

What is getattr() Function in Python

getattr() default function example

in this example we are going to discuss how the getattr function call the default attribute of function in this example we are going to create class of employ which is working in an Office.

This employ’s class comes with these attributes, employ ID, employ name and employ salary, and let’s say if one of the attributes is not present in the class then the function will return the value with the default value function,

Let’s write an example code using getattr() default function, let’s say if the salary is not declared in the class as an attribute then it will say you that attribute is not found and will display the default salary or they employ,

Default Code Example

class Student:

employ_ID=””

employ_name=””

# initial constructor to set the values

def __init__(self):

self.employ_ID = “452352”

self.employ_name = “Ammar”

student = Student()

# using default value option

print(‘Using default value : salary of the employ is =’, getattr(student, “student_cgpa”, 500.00))

# without using default value

try:

print(‘Without default value : salary of the employ is =’, getattr(student, “student_cgpa”))

except AttributeError:

print(“Attribute is not found :(“)

Advantages of getattr()

There are many types of advantages of this function but there are two major advantages that we can use in many places in python,

The very first one is, it will let you access the attributes from the string, for example, if there are many types of attributes available in the class, and we might be depending on the circumstance. We can hard-code them all, but it’s really often easier to use a list or dict of strings instead.

And the Second is it will lets you define an optional or you may say default value to return if the attribute does not exist in the class, So in order to handle the exception when the attribute does not exist, we may want to display or return the value of None or an empty list etc.

LEAVE A REPLY

Please enter your comment!
Please enter your name here