Python getattr( ) Function


The vast variety of Python built-in modules, functions, and statements helps programmers to perform various tasks. The getattr() function is a Python built-in function that allows programmers to access the attribute value of an object. If the value is not found, then the getattar() function returns the default value. This is the reason why the getattr() function is used mostly to access the attribute values of objects. This article will provide a detailed explanation of the getattr() function with some examples.

Syntax

Before moving on to the implementation of the getattr() function, first, we will discuss its syntax. The syntax of the getattr() function is as follows:

getattr(object_name,attribute_name,defalut_value)

The getattr() function takes three parameters as an argument:
object_name: The name of the object whose attribute we need to access.
attribute_name: The name of the attribute that we need to access.
default_value: The default value that is returned when the attribute is not found.

If the name attribute is not found and we do not define the default_value, the getattar() function raises an AttributeError exception.

Examples

Now, we will look at some examples using the getattr() function. We have created a student class and defined some attributes for this class. We are accessing these attributes using the getattar() function.

#creating a student class
class Student:
    #defining name attribute
    name ="John"
    #defining an email attribute
    email="john@example.com"
#Creating a student class object
std_obj = Student()
#now std_obj is our object
#accessing the name attribute
print("The name attribute value is: ",getattr(std_obj,'name'))
#accessing the email attribute
print("The email attribute value is: ",getattr(std_obj,'email'))

Output

The getattr() function returned the attribute values successfully.

Now, we will try to access an attribute that is not defined. We will define the default value for this missing attribute.

#creating a student class
class Student:
    #defining name attribute
    name ="John"
    #defining an email attribute
    email="john@example.com"
#Creating a student class object
std_obj = Student()
#now std_obj is our object
#accessing the age attribute
print("The age is: ",getattr(std_obj,'age','Above 20'))

The defined value is “Above 20”.

Output

In the case of the missing ‘age’ attribute, the defined value is printed.

If the value is not defined, the getattr() function will raise an ‘AttributeError’ exception. Let us see an example of this.

#creating a student class
class Student:
    #defining name attribute
    name ="John"
    #defining an email attribute
    email="john@example.com"
#Creating a student class object
std_obj = Student()
#now std_obj is our object
#accessing the age attribute
print("The age is: ",getattr(std_obj,'age'))

 Output

You can also use the getattr() function with namedtuple to access the values. The namedtuple is a dictionary-type container, but you can access its values using the getattar() function. The namedtuple is a class of collections module. Let us implement a namedtuple and access the values using the getattr() function. We are creating a namedtuple for the student.

#importing the collections module
import collections
#creating a namedtuple for a student
#name and age are the keys
Student= collections.namedtuple('Student',['name','age'])
#creating a new teacher and adding the values
std = Student("John",21)
print("The name is:" ,getattr(std,"name"))
print("The age is: ",getattr(std,"age"))

Output

Conclusion

The getattar() function is a built-in function in Python that returns the value of an object attribute. The primary reason to use the getattr() function is that it makes it easy to access the value of an attribute using the attribute’s name. The attribute name is defined as a string, and we get the value using the getattar() function. A value can also be defined for the missing attribute. After reading this article, hopefully, you learned more about how to use the Python getattar() function.



from Linux Hint https://ift.tt/3luScE0

Post a Comment

0 Comments