Hacking Secret Ciphers with Python

(Ann) #1

146 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


When you assign the reference in spam to cheese, the cheese variable contains a copy of the
reference in spam. Now both cheese and spam refer to the same list.


Figure 10-3. Changing the list changes all variables with references to that list.

When you alter the list that cheese refers to, the list that spam refers to is also changed because
they refer to the same list. If you want spam and cheese to store two different lists, you have to
create two different lists instead of copying a reference:





spam = [0, 1, 2, 3, 4, 5]
cheese = [0, 1, 2, 3, 4, 5]





In the above example, spam and cheese have two different lists stored in them (even though
these lists are identical in content). Now if you modify one of the lists, it will not affect the other
because spam and cheese have references to two different lists:





spam = [0, 1, 2, 3, 4, 5]
cheese = [0, 1, 2, 3, 4, 5]
cheese[1] = 'Hello!'
spam
[0, 1, 2, 3, 4, 5]
cheese
[0, 'Hello!', 2, 3, 4, 5]




Free download pdf