Open In App

How to Change Class Attributes in Python

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 “GeeksforGeeks“. After direct assignment, we print the original and edited values, printing the update to “GFG“.

Python3




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


Output

Original Value: GeeksforGeeks
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 “GeeksforGeeks“. After using setattr(), we print the original and edited values, demonstrating the modification to “GFG“.

Python3




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


Output

Original Value: GeeksforGeeks
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 = "GeeksforGeeks"
    @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: GeeksforGeeks
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 = "GeeksforGeeks"
    @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: GeeksforGeeks
Edited Value: GFG


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads