"""
Don't use m * [n * [0]]
to create an array. Details follow.
Two objects are the same if and only if they have the same id
number. in the first example, x
and y
are different objects with the same
value [0]
. The is
operator compares the ids.
"""
watchall('x = y = [0]') print # In this example, x and y are different objects. watchall('x = [0]; y = [0]') print # The "is" operator compares id numbers. x = y = 1 print x is y x = [0]; y = [0] print x is y print # In Python, it is tempting to write "arr = 3 * [4 * [0]]" to create a 3 by # 4 array. But every newbie soon learns not to do this but instead to write # a loop. x = 2 * [3 * [0]] print x x[0][0] = 1 print x print # I use the following which completely avoids the "*" construction. arr = [] for col in range(2): row = [] for r in range(3): row.append(0) arr.append(row) print arr arr[0][0] = 1 print arr