Did you know that if you make an abbreviated sum (+=) of lists does not modify the id of the variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

Did you know that if you make an abbreviated sum (+=) of lists does not modify the id of the variable

When doing the normal sum, a = a + [1] modifies the id of the variable and when doing the abbreviated sum, a += [1] does not modify it when changing the id of the variable, the modified list is different and will not add the list indicated after the operator to the original reference a, b = [0], [0] ra, rb = a, b a = a + [1] b += [1] print (a is ra) #output False print (b is rb) #output True https://code.sololearn.com/cXGyKj6C1U9c

25th Feb 2018, 2:27 AM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
3 Answers
+ 3
thank U,for mutable object , operator + call __add__ method and create a new object ,operator += call __iadd__ method just update the original object,so b is the same with rb.
25th Feb 2018, 1:41 PM
capsbon
+ 2
Now I know! - Thanks
25th Feb 2018, 11:12 AM
Louis
Louis - avatar
+ 1
hi
25th Feb 2018, 4:52 AM
شركه الانستقرام
شركه الانستقرام - avatar