3 ms·
I'd say it's even deeper - the += operator is fundamentally tied with Python's semantics. Consider: >>> a = [1, 2] >>> b = a >>> a = a + [3] >
by yongjik 6y ago
I'd say it's even deeper - the += operator is fundamentally tied with Python's semantics. Consider:
>>> a = [1, 2]
>>> b = a
>>> a = a + [3]
>>> b
[1, 2]
It has to be [1, 2] because the third line creates a new object. If a + operation changes its operand, then the concept of object identity breaks down - at least, it will be a very different language.
- pansa2 6y agoYou’re right - I’m not suggesting that `+` should work in-place, but that `a += b` should do the same thing as `a = a + b` and not work in-place.
- BerislavLopac 6y agoSo you're saying that a += b should create a new object and assign it to a? Now that would be even more unexpected...
- pansa2 6y agoI don’t think it’s unexpected for an augmented assignment operator to do an assignment. It is unexpected for it to instead call `extend`. It’s even worse to do the first for some types and the second for other types.