Re:Examples of Incorrect Abstractions in Other Languages
by rozab on Wednesday May 20, 2020 @05:53AM
(#23245039)
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.
[ Parent
| on HN ]
Re:Examples of Incorrect Abstractions in Other Languages
by pansa2 on Wednesday May 20, 2020 @07:47AM
(#23245726)
> 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`.
Re:Examples of Incorrect Abstractions in Other Languages
by tsegratis on Wednesday May 20, 2020 @07:24AM
(#23245578)
Agreed, 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
Re:Examples of Incorrect Abstractions in Other Languages
by throwaway_pdp09 on Wednesday May 20, 2020 @08:08AM
(#23245871)
> 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.
Re:Examples of Incorrect Abstractions in Other Languages
by theelous3 on Wednesday May 20, 2020 @07:28AM
(#23245598)
You 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.