Open In App

How to Change Class Attributes By Reference in Python

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We have the problem of how to change class attributes by reference in Python, we will see in this article how can we change the class attributes by reference in Python.

What is Class Attributes?

Class attributes are typically defined outside of any method within a class and are shared among all instances. To change these attributes by reference, you can access and modify them directly through the class name.

Syntax :

class MyClass:

class_attribute = “Original Value”

# Changing class attribute by reference

MyClass.class_attribute = “New Value”

Change Class Attributes By Reference in Python

Below are some of the methods for how to change class attributes by reference in Python:

  • Numeric Class Attribute
  • List Class Attribute
  • Dictionary Class Attribute
  • Class Attribute Using Method

Change Numeric Class Attribute By Reference in Python

In this example, the count class attribute is incremented by reference, and the change is reflected in all instances of the Counter class.

Python3




class Counter:
    count = 0
 
# Increase count by reference
Counter.count += 1
 
# Accessing the updated value
print(Counter.count) 


Output

1

Change List Class Attribute By Reference in Python

In this example, in below code the shared_list class attribute is a list that is modified by reference through the append method. The change is visible in all instances of the SharedList class.

Python3




class SharedList:
    shared_list = []
 
# Append an element by reference
SharedList.shared_list.append(42)
 
# Accessing the updated list
print(SharedList.shared_list)


Output

[42]

Change Dictionary Class Attribute By Reference in Python

In this example, the settings class attribute is a dictionary. By updating a key in the dictionary by reference, the change is propagated to all instances of the Config class.

Python3




class Config:
    settings = {"mode": "normal", "debug": False}
 
# Update a key in the dictionary by reference
Config.settings["debug"] = True
 
# Accessing the updated dictionary
print(Config.settings) 


Output

{'mode': 'normal', 'debug': True}

Change Class Attribute Using Method By Reference in Python

In this example, the interest_rate class attribute is modified by reference through a class method (increase_interest_rate). This method allows for a more controlled and organized way of updating class attributes.

Python3




class BankAccount:
    interest_rate = 0.03
 
    @classmethod
    def increase_interest_rate(cls, percentage):
        # Modify class attribute by reference through a method
        cls.interest_rate += percentage
 
# Increase interest rate by 1%
BankAccount.increase_interest_rate(0.01)
 
# Accessing the updated interest rate
print(BankAccount.interest_rate) 


Output

0.04

Conclusion

In Conclusion, changing class attributes by reference in Python provides a powerful mechanism for maintaining consistency, centralizing control, and simplifying code. It ensures that modifications to shared variables are reflected across all instances, promoting efficiency and clarity in object-oriented programming. Understanding and leveraging this capability can lead to more robust and maintainable code in various Python application.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads