Game Development Reference
In-Depth Information
10 - Tic Tac Toe
When you alter the list that
cheese
refers to, the list that
spam
refers to is also changed
because they are 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, 'Hello!', 2, 3, 4, 5]
>>> cheese
[0, 1, 2, 3, 4, 5]
Figure 10-8 shows how the two references point to two different lists:
Figure 10-8: Two variables each storing references to two different lists.