Change Class Attributes in Python

Below are the ways to edit class attributes in Python:

  • Using Direct Assignment
  • Using setattr() function
  • Using Class method
  • Using a property decorator

Edit Class Attributes Using Direct Assignment

In this example, we use the direct assignment approach to edit the class attribute website of the Geeks class, initially set to “w3wiki“. After direct assignment, we print the original and edited values, printing the update to “GFG“.

Python3




class Geeks:
    website = "w3wiki"
 
# orginal value
print("Original Value: " + Geeks.website)
 
# editing using direct assignment
Geeks.website = "GFG"
print("Edited Value: " +Geeks.website)


Output

Original Value: w3wiki
Edited Value: GFG

Edit Class Attributes Using setattr() function

In this example, we use the setattr() function to dynamically edit the class attribute website of the Geeks class, initially set to “w3wiki“. After using setattr(), we print the original and edited values, demonstrating the modification to “GFG“.

Python3




class Geeks:
    website = "w3wiki"
 
# original value
print("Original Value: " + Geeks.website)
 
# editing using setattr()
setattr(Geeks, 'website', 'GFG')
print("Edited Value: " +Geeks.website)


Output

Original Value: w3wiki
Edited Value: GFG

Edit Class Attributes Using Class method (@classmethod)

In this example, we define a class method update_website decorated with @classmethod within the Geeks class, allowing us to modify the class attribute website. The original value is printed, and then the class method is utilized to update the attribute to “GFG“, showcasing the edited value.

Python3




class Geeks:
    website = "w3wiki"
    @classmethod
    def update_website(cls, new_value):
        cls.website = new_value
# original value
print("Original Value: " + Geeks.website)
# editing using a class method
Geeks.update_website('GFG')
print("Edited Value: " +Geeks.website)


Output

Original Value: w3wiki
Edited Value: GFG

Edit Class Attributes Using a property decorator

In this example, the Geeks class uses a property decorator to create a getter and setter for the private attribute _website. The original value is obtained using the getter, and then the property setter is used to update the attribute to “GFG“. The final value is printed, demonstrating the successful edit.

Python3




class Geeks:
    _website = "w3wiki"
    @property
    def website(self):
        return self._website
    @website.setter
    def website(self, value):
        self._website = value
# original value
obj = Geeks()
print("Original Value: " + obj.website)
# editing using property setter
obj.website = 'GFG'
print("Edited Value: " +obj.website)


Output

Original Value: w3wiki
Edited Value: GFG


How to Change Class Attributes in Python

In Python, editing class attributes involves modifying the characteristics or properties associated with a class. Class attributes are shared by all instances of the class and play an important role in defining the behavior and state of objects within the class. In this article, we will explore different approaches through which we can edit class attributes in Python.

Similar Reads

Change Class Attributes in Python

Below are the ways to edit class attributes in Python:...

Contact Us