Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  Python

A Python assignment trap

  sonic0002      2013-07-19 22:08:36      7,055    0    0

Python has no assignment, it only has reference. Assume, we have following code snippet:

>>> values = [0, 1, 2]
>>> values[1] = 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. 

When run:

values[1] = 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[1] 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[1] = 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[0] = 8
a[1][1] = 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[0] = 8
a[1][1] = 9

Reference :



Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



No comment for this article.


When using frameworks in small project

By sonic0002