From the Python documentation:
An object of an immutable sequence type cannot change once it is created. (If the object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change.)
While the first part of the original quote is understandable, I don't quite get the last sentence. How can I understand the phrase: "directly referenced by an immutable object"?
Probably the best way to explain this will be an example.
>>> a = [1, 2, 3]
>>> b = (a, a)
b is a tuple -- an immutable sequence. However, the objects it contains are lists, which are mutable.
It's possible to modify the mutable objects in the collection:
>>> a.append(4)
>>> b
([1, 2, 3, 4], [1, 2, 3, 4])
However, it isn't possible to change which objects exist in a, because it's immutable. The first and second elements of a will always refer to the same list, and there's no way to change that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With