The Python Apprentice
上QQ阅读APP看书,第一时间看更新

Assigning one reference to another

When we assign from one variable to another, what we're really doing is assigning from one object reference to another object reference, so that both references then refer to the same object. For example, let's assign our existing variable x to a new variable y:

>>> y = x

That gives us this resulting reference-object diagram:

Figure 4.3: Assign the existing name 'x' to the name 'y'

Now both references refer to the same object. We now reassign x to another new integer:

>>> x = 3000

Doing this gives us a reference-object diagram showing our two references and our two objects:

Figure 4.4: Assign a new integer 3000 to 'x'

In this case there is no work for the garbage collector to do because all of the objects are reachable from live references.