Immutable vs Mutable Types: A Deeper Understanding
The difference between immutable and mutable types can be a source of confusion, especially when it comes to certain classes that inherit from immutable types.
Immutable Types: Unchanging by Design
In Python, immutable types are objects whose value cannot be modified in place. Once created, an immutable object remains unchanged; any attempt to modify it results in a new object. For example, the float object is immutable, meaning that it cannot be altered directly.
SubclassingImmutable Types: Creating the Illusion of Mutability
However, it is possible to create subclasses of immutable types that appear to be mutable. This is achieved by overriding the __new__ method. For instance, the RoundFloat class is a subclass of float that rounds its value to two decimal places:
class RoundFloat(float): def __new__(cls, val): return float.__new__(cls, round(val, 2))
While this code defines a new class that inherits from float, the RoundFloat object remains immutable. This is because the __new__ method simply creates a new float object with the rounded value, and it does not alter the original float object.
Mutable Types: Changing on the Inside
In contrast, mutable types allow their values to be modified directly. Classes that define methods can be considered mutable. For example, the SortedKeyDict class inherits from dict and defines an example() method:
class SortedKeyDict_a(dict): def example(self): return self.keys()
This class allows the modification of its values within the provided methods. However, it's important to note that the SortedKeyDict class itself is still immutable. Its values can be changed, but the underlying instance remains the same.
Understanding Mutability in Practice
To better understand the difference between immutable and mutable types, consider the following scenario. Passing the set d to the SortedKeyDict class with __new__ raises an error, while passing it to the RoundFloat class with __new__ does not.
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2)) SortedKeyDict.__new__(cls, d) # Error raised RoundFloat.__new__(cls, 12) # No error
This illustrates that SortedKeyDict is mutable and RoundFloat is immutable. Because SortedKeyDict is mutable, it can be modified in place within its methods. In contrast, RoundFloat is immutable, so passing it to __new__ does not modify the original object.
The above is the detailed content of Immutable vs. Mutable Types in Python: When Does Subclassing Break Immutability?. For more information, please follow other related articles on the PHP Chinese website!