+ 2

How can you explain difference between a+=4 and a=a+4?

https://code.sololearn.com/cNOuWV9XD2Q2/?ref=app The list b remains unaffected if you use second assignment method.

23rd Dec 2020, 1:17 AM
Pranjal Tambe
Pranjal Tambe - avatar
1 Odpowiedź
+ 6
a= redefines a to a new object where a+= mutates the current object held by a. In other words a points to the original list which b=a means that b will also point to that same original list. Any mutation using a or b will change the original list. When a= is used a will now point to a new object that is created using the old objects values with the [5] appended to it.
23rd Dec 2020, 1:34 AM
ChaoticDawg
ChaoticDawg - avatar