What's going on with my Python variable? old_pos
seems to be linked to pos
:
Code:
pos = [7, 7]
direction = [1, 1]
old_pos = pos
print 'pos = '+str(pos)
print 'old_pos = '+str(old_pos)
pos[0] += direction[0]
pos[1] += direction[1]
print 'pos = '+str(pos)
print 'old_pos = '+str(old_pos)
Output:
pos = [7, 7]
old_pos = [7, 7]
pos = [8, 8]
old_pos = [8, 8]
However, if I replace old_pos = pos
with old_pos = tuple(pos)
or even old_pos = list(pos)
, I don't get this problem:
pos = [7, 7]
old_pos = [7, 7]
pos = [8, 8]
old_pos = [7, 7]