Still one of the oldest #Python oddities I've ever seen 🐍🤔
>>> a = ([],)
>>> a[0] += ["what"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a
(['what'],)
Note that an exception was raised but the operation also worked! 😳 #pythonoddity
@treyhunner Ha! That's a good one, it really illustrates how `+=` is just syntactic sugar over the `+` and the `=`
@chris @treyhunner @s_gruppetta
It's weirder than that!
`a[0] = a[0] + ["what"]` reports the error but doesn't have the side effect.
`a[0] += ["what"]` reports the error and DOES have the side effect!
I'm shocked both that the failed assignment has a side effect and that these do not behave the same way. Can anyone explain?
@s_gruppetta @chris @treyhunner Fascinating!
I can imagine efficiency reasons for these being different.
It still seems wrong that __iadd__ for tuples makes a change and *then* throws an error. Is there a reason for this behavior?
@peterdrake @s_gruppetta @chris it's actually __iadd__ on a list that's being used here.
a[0] += ["huh"]
Checks if a[0] has a __iadd__ method, which of it does because it's a list. So that line is translated to:
a[0] = a[0].__iadd__(["huh"])
The list __iadd__ method succeeds.
But then assigning to a[0] (which happens after the right-hand side of that assignment) fails.