3 ms·
See https://github.com/satwikkansal/wtfpython https://github.com/satwikkansal/wtfpython "Mutating the immutable" is my personal favourite - the second line of
by pansa2 4y ago
See https://github.com/satwikkansal/wtfpython https://github.com/satwikkansal/wtfpython
"Mutating the immutable" is my personal favourite - the second line of code fails with an exception despite having already succeeded:
>>> t = ([1, 2], 5, 6)
>>> t[0] += [3, 4]
TypeError: 'tuple' object does not support item assignment
>>> print(t)
([1, 2, 3, 4], 5, 6)
I've never been a fan of the decision to have `a += b` sometimes do `a = a + b` and sometimes update `a` in place. My objection was initially just due to theoretical inelegance - but this code shows that decision directly causing a concrete problem.
- masklinn 4y agoSadly the core team decided to not learn from that mistake, and did the exact same thing with the dict merge operator.
- omaranto 4y agoBryce Adelstein Lelbach, the ISO C++ Library evolution chair, says that he would rather be consistently wrong than inconsistently right. Maybe that's what the Python core team thought, "we'd better make this consistent with +=, whose quirks people already know".
- masklinn 4y agoThat strikes me as a turbocharged version of the sunk cost fallacy.