I cannot find brief information about what is happening in this very simple program:
print 'case 1' # a and b stay different a = [1,2,3] b = a b = [4,5,6] print 'a =',a print 'b =',b print print 'case 2' # a and b becomes equal a = [1,2,3] b = a b[0] = 4 b[1] = 5 b[2] = 6 print 'a =',a print 'b =',b print print 'case 3' # a and b stay different now a = [1,2,3] b = a[:] b[0] = 4 b[1] = 5 b[2] = 6 print 'a =',a print 'b =',b print print 'case 4' # now the funny thing a=[1,2,[3]] b=a[:] b[0] = 4 b[1] = 5 b[2][0] = 6 # this modifies b and a!!!
The result of this simple test:
case 1 a = [1, 2, 3] b = [4, 5, 6] case 2 a = [4, 5, 6] b = [4, 5, 6] case 3 a = [1, 2, 3] b = [4, 5, 6] case 4 a = [1, 2, [6]] b = [4, 5, [6]]
I clearly don't understand how python handles each case. Can someone provide a link so I can read about it, or a short explanation of what is going on?
Many thanks.