"""Exploring lists as mutable objects
http://openbookproject.net/thinkcs/python/english2e/ch09.html#lists-are-mutable
"""
a = "banana"
b = "banana"
# do a and b point to the same object in memory?
id(a)
id(b)
# Python only created one string, and both a and b refer to it.
# lists behave differently. When we create two lists, we get two objects:
a = [1, 2, 3]
b = [1, 2, 3]
id(a)
id(b)
# Since variables refer to objects, if we assign one variable to another,
# both variables refer to the same object:
a = [1, 2, 3]
b = a
id(a) == id(b)
# if we change b, that also changes a
b[0] = 5
print a
# If we want to modify a list and also keep a copy of the original, we need
# to be able to make a copy of the list itself, not just the reference.
# This process is sometimes called cloning, to avoid the ambiguity of the word copy.
# The easiest way to clone a list is to use the slice operator:
a = [1, 2, 3]
b = a[:]
print b
a == b
id(a) == id(b)
# the equality operator for lists determines if all elements of the two lists are the same
# Now we are free to make changes to b without worrying about a:
b[0] = 5
print a
