5.4 Understanding variables, references, and assignment
How do variables really work? What happens when we assign a mutable object to two variables? When two variables are sharing references to a common mutable object, the behaviors can be confusing.
This is the core principle: Python shares references; it doesn’t copy data.
To see what this rule on reference sharing means, we’ll create two data structures: one is mutable and one is immutable.
5.4.1 Getting ready
We’ll look at the two kinds of sequences, although we could do something similar with two kinds of sets:
>>> mutable = [1, 1, 2, 3, 5, 8]
>>> immutable = (5, 8, 13, 21)
We’ll look at what happens when references to these objects are shared.
We can do a similar comparison with a set and a frozenset. We can’t easily do this with a mapping because...