9 ms·
Because I once learned that `a += b` is just a shortcut for `a = a + b`, just a syntax sugar. Now I have to constantly remind myself that it's not the case in p
by perfunctory 7y ago
Because I once learned that `a += b` is just a shortcut for `a = a + b`, just a syntax sugar. Now I have to constantly remind myself that it's not the case in python.
edit: I might have misunderstood your question. If you meant "why it's not equivalent" please see the falkaer's answer.
- im3w1l 7y agoThey aren't equivalent in C++ either.
- perfunctory 7y agoMy C++ is a bit rusty. Does it manifest itself in the standard library as well? The problem with python (in my view) is that this behaviour is implemented in the standard library and therefor propagates to the 3rd-party libraries by convention.
- zabzonk 7y ago> Does it manifest itself in the standard library as well? Yes. For example for std::string `a = a + b;` will (at least notionally) create a temporary string and then use the assignment operator. `a += b;` will not do this.
- cultureswitch 7y agoIt's not equivalent but honestly this gotcha with lists will only show up in bad code and interview quizzes (i.e. bad code).