Clarification on the reason of failed excesrise

Yes, that’s correct. That is the meaning of “-=” from an arithmetic p.o.v. But what’s really going on here is more complicated than that. The numeric result is the same, but if the operands in question are “objects” (meaning pointers) then the way memory is managed is very different. The “-=” operator is “in place”, meaning that it directly modifies the object in memory. The plain assignment allocates a new memory object for the RHS, so the original variable is not modified in memory. If the variable in question is an object passed as a parameter to a python function and you didn’t first “copy” it, then you’ve modified global data with the “-=” approach. The test cases for this exercise are written in such a way that modifying the global data causes subsequent tests to fail.

The thread I linked explains this in more detail.

1 Like