Hacking Secret Ciphers with Python

(Ann) #1
Chapter 10 – Programming a Program to Test Our Program 147

Figure 10- 4 shows how the two references point to two different lists:


Figure 10- 4. Two variables each storing references to two different lists.

The copy.deepcopy() Functions


As we saw in the previous example, the following code only copies the reference value, not the
list value itself:





spam = [0, 1, 2, 3, 4, 5]
cheese = spam # copies the reference, not the list





If we want to copy the list value itself, we can import the copy module to call the
copy.deepcopy() function, which will return a separate copy of the list it is passed:





spam = [0, 1, 2, 3, 4, 5]
import copy
cheese = copy.deepcopy(spam)
cheese[1] = 'Hello!'
spam
[0, 1, 2, 3, 4, 5]
cheese
[0, 'Hello!', 2, 3, 4, 5]




Free download pdf