a += b is not always a = a + b? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a += b is not always a = a + b?

Operator "in-place: +=" gives an unexpected result. Why is that? Please comment the following code. https://code.sololearn.com/cJfDR5W6knpc/?ref=app

7th Oct 2019, 6:34 AM
rez cebov
rez cebov - avatar
2 Answers
0
This question was asked by Pratik Joshi, but without code.
7th Oct 2019, 6:36 AM
rez cebov
rez cebov - avatar
0
Lists are always passed by reference. If you append something to list1 with +=, list2 is still a reference to list1. With = you define a new list1, while list2 keeps the reference to the old list.
7th Oct 2019, 6:10 PM
Chris
Chris - avatar