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`.
This is true for lists in Python. `a = a + b` creates a new list, but `a += b` works in-place:
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`.