Python variable processing, I don't understand it

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.

+6
source share
4 answers

This is a fantastic visualization tool for python code .

Run your code and everything should be clear in a minute.

+9
source

There are two important things here:

  • Variables are just labels that point to objects.
  • The list is modified in python, and integers are not.

When you find that both a and b changed, it is because both of them point to the same object. You can do id(a) and id(b) to confirm this.

For examples, note that a[:] will create a new object, which is a copy of a . However, it will be a shallow copy, not a deep copy. This explains why in example 4 you can change both a and b . They point to different list objects, but one element is another list that is shared by both of them.

+6
source

Case 1: name b rebound.

Case 2: a and b tied to the same object.

Case 3: A shallow copy of a is associated with b . The lists are different, but the objects in the list are the same.

Case 4: An invalid copy of a bound to b , and then one of the objects is mutated.

Re-binding does not mutate, and the mutation is not restored.

+4
source
 print 'case 1' # a and b stay different a = [1,2,3] b = a #At this point 'b' and 'a' are the same, #just names given to the list b = [4,5,6] #At this point you assign the name 'b' to a different list print 'a =',a print 'b =',b print print 'case 2' # a and b becomes equal a = [1,2,3] #At this point 'b' and 'a' are the same, #just names given to the list b = a b[0] = 4 #From here you modify the list, since both 'a' and 'b' #reference the same list, you will see the change in 'a' b[1] = 5 b[2] = 6 print 'case 3' # a and b stay different now a = [1,2,3] b = a[:] #At this point you COPY the elements from 'a' into a new #list that is referenced by 'b' b[0] = 4 #From here you modify 'b' but this has no connection to 'a' 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[:] #At this point you COPY the elements from 'a' into a new #list that is referenced by 'b' b[0] = 4 #Same as before b[1] = 5 b[2][0] = 6 # this modifies b and a!!! #Here what happens is that 'b[2]' holds #the same list as 'a[2]'. Since you only modify the element of that #list that will be visible in 'a', try to see it as cases 1/2 just #'recursively'. If you do b[2] = 0, that won't change 'a' 
+3
source

Source: https://habr.com/ru/post/909185/


All Articles