Today's Question: Hide Googleâ€™s director of engineering Ray Kurzweil recently claimed that by 2029 computers would be able to understand our language, learn from experience and outsmart even the most intelligent humans. We could see many these scene in movies that smart robot which can do many things humans can do. Do you believe that one day computers will be smarter than humans who create them? What will it happen if computers are smarter than humans? Will computer be smarter than humans? Description My Answer

Technical Article => Programming =>  Python

## A Python assignment trap

Source : sonic0002    Date : 2013-07-19 22:08:36

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```

Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".

Tags : Python,Assignment,Trap,Shallow copy   Read(2781) Comment(0) Version(0)

Previous : Social media sharing codes
Next : bash network interface

::Related Articles

::Comment Zone  (Total 0 comment)

::Comment

 Nickname Email Comment

:: Other versions

No other versions available yet.

:: Recent articles