[Python-ideas] Adding "+" and "+=" operators to dict
Chris Barker - NOAA Federal
chris.barker at noaa.gov
Sat Feb 14 01:32:46 CET 2015
>> But the "problem" here is that augmented assignment shouldn't work on
>> immutables at all.
>
> The problem is that the way augmented assignment works is to first treat the target as an expression, then call __iadd__ on the result, then assign the result of that back to the target.
Right - I figured that out, and found the FAQ. But why does it work
that way? Because it needs to work on immutable objects. If it didn't,
then you wouldn't need the "assign back to the original name" step.
Let's look at __iadd__:
If it's in-place for a mutable object, it needs to return self. But
the python standard practice is that methods that mutate objects
shouldn't return self ( like list.sort() ) for instance.
But if you want the augmented assignment operators to support
immutables, there needs to be a way to get a new object back -- thus
the bind, and the confusing, limiting behavior.
But it's what we've got.
Is there a lesson here for the additional unpacking being proposed? I
have no idea.
-Chris
- CHB
More information about the Python-ideas
mailing list