3 ms·
Python is a language that seems to make a lot of sense at first but gets a bit weird when you dig into things. The + operator particularly is a bit funny; I rec
by rozab 6y ago
Python is a language that seems to make a lot of sense at first but gets a bit weird when you dig into things. The + operator particularly is a bit funny; I recently came across a case where x+=y was not equivalent to x=x+y... Though I can't figure out what it was now.
- tsegratis 6y agoAgreed, but seems haskell also fails to keep commutativity here tho? Concatenation is not addition in my opinion I've often wanted something like bits++bits concatenation to work in the same way for numbers as for strings
- throwaway_pdp09 6y ago> Concatenation is not addition in my opinion For python, syntactically it is. In a general sense, an operation is what you define it to be. Just my opinion.
- theelous3 6y agoYou were likely doing something with threads and failed to lock `x`, which I would describe as an issue that should be expected. It doesn't really have to do with `+=` so much as it has to do with being aware of race conditions on variable unprotected by a mutex.
- G4E 6y agoPython's list sum are funny like that. x+=y is equivalent to x.append(y) x = x + y creates a new list, then assign it to x (highly inefficient in a loop)
- pansa2 6y ago> a case where x+=y was not equivalent to x=x+y This is true for lists in Python. `a = a + b` creates a new list, but `a += b` works in-place: >>> a = [1, 2] >>> b = a >>> a = a + [3] >>> print(a, b) [1, 2, 3] [1, 2] >>> a = [1, 2] >>> b = a >>> a += [3] >>> print(a, b) [1, 2, 3] [1, 2, 3] There are other issues with the `+=`-style operators as well - IMO it was a mistake to have `a += b` be anything other than a shorthand for `a = a + b`.