| by Arround The Web | No comments

Python Setattr() Function

An essential feature of Python is the ability to set attribute values in the class by referring to the created class objects at runtime. Attributes are variables or functions that belong to an object and can be accessed/called through the dot notation, such as “object.attribute”. The “setattr()” is a built-in function in Python that sets or assigns an object’s attribute value.

This post will explain a complete guide on the “setattr()” function using numerous examples via the following supported content:

What is the “setattr()” Function in Python?

In Python, the “setattr()” function is used to assign the specified attribute value of the target object.

Syntax

setattr(object, attribute, value)

 
Parameters

In the above syntax:

    • The “object” parameter specifies an object whose attribute needs to be set.
    • The “attribute” parameter indicates the attribute name.
    • The “value” parameter denotes the value we want to assign to the attribute.

Return Value

The “setattr()” function returns no value.

Example 1: Applying the “setattr()” Function to Set the Attribute Value

The below code is used to set/modify the attribute value of the specified object:

class students:
  name = "Joseph"
  age = 55
  height = 9.5

p1 = students()
print('Before Setting Attribute: ', p1.age)
setattr(p1, 'age', 40)
print('After Setting Attribute: ',p1.age)

 
In the above code, the “setattr()” function takes the created class (students) object “p1”, attribute “age”, and value “40” as its arguments, respectively, and modifies the target value accordingly.

Output


The attribute value has been modified successfully.

Example 2: Applying the “setattr()” Function to Set the Not Existent Attribute Value

The below code shows how the “setattr()” function works with the attribute that is not found:

class students:
  name = "Joseph"
  age = 55
  height = 9.5

p1 = students()
setattr(p1, 'name', 'Lily')
print(p1.name)

setattr(p1, 'sex', 'male')
print(p1.sex)

 
In the above code lines, the “setattr()” function sets the new value to the attribute “name” that is present in the class. After that, the “setattr()” function also assigns the value to the attribute “sex” that is not contained in the class.

Output


As seen, the non-existent attribute is created and returned based on its set value using the “setattr()” function.

Example 3: Alternative Way of Setting an Attribute of Python Object

The below code is used to set/assign an attribute using the “dot” notation syntax with the “self” parameter:

class students:
    def __init__(self, name, age, height):
        self.name = name
        self.age = age
        self.height = height
   
p1 = students('Joseph', 43, 5.5)
p1.age = 22
p1.sex = 'Male'
print('Attribute "Age" Value: ', p1.age )
print('Attribute "Sex" Value: ', p1.sex)

 
In the above code block, the “attribute” named “age” that exists in the class is set/modified, and the “attribute” named “sex” that does not exist is created and assigned a value using the dot syntax. Finally, both attributes are returned.

Output

Conclusion

Python’s “setattr()” function sets the specified object’s attribute value. This function creates an attribute that is not contained in the class. The “setattr()” function modifies the attribute of the Python object by accepting the object, attribute, and value as its arguments, respectively. This tutorial presented a detailed post on Python’s “setattr()” function using several examples.

Share Button

Source: linuxhint.com

Leave a Reply