A Python assignment trap
Python has no assignment, it only has reference. Assume, we have following code snippet:
>>> values = [0, 1, 2] >>> values = values >>> values [0, [...], 2]
Why the result is not [0, [0, 1, 2], 2], instead it goes into an infinite loop? To understand this, we need to understand some basics about Python.
Python has no variables, it only has labels. When we run:
values = [0, 1, 2]
Python will first create a list object [0,1,2], then it labels it as values. If we later run:
values = [3, 4, 5]
Python will then create a new list object [3,4,5] and it will tear off the values label from [1,2,3] and label [3,4,5] as values.
In this process, there is no a values list object container, Python doesn't copy any object to values as well.
values = values
The second element in the list labeled with values will point to the list object itself referenced by values. The values label will still point to the list object, the only change is the structure of the list object content. It changes from [0,1,2] to [0,?,2], the ? is a reference pointing to the list object.
To get the expected result [0, [0, 1, 2], 2], we need to create another copy of the [0,1,2] object, then we point values to the newly created object. The method to copy an object is different for different object types. To copy a list, we can use:
values = values[:]
Python will first dereference the object referenced by values, then it will run [0,1,2][:] to create a new list object with the same contents. Then the second element in values will point to the newly created list object.
To give you more details, values[:] only does shallow copy, when there is nesting in the list, the result will not be as expected.
a = [0, [1, 2], 3] b = a[:] a = 8 a = 9
What will be values of a and b? a will be [8, [1, 9], 3] and b will be [0, [1, 9], 3]. The element in b is also changed.
To do a deep copy, we need to run:
import copy a = [0, [1, 2], 3] b = copy.deepcopy(a) a = 8 a = 9
If you woke up tired, you need more sleep. If you woke up drooling on your desk, you need a new job. If you woke up with a headache, somewhere in clarke quay, with dried mee goreng stuck to your face, you probably need answers, and a drink. Why not this product, huh? It's got potassium and b vitamin