[Python-ideas] Adding "+" and "+=" operators to dict

Chris Barker - NOAA Federal chris.barker at noaa.gov
Fri Feb 13 17:02:56 CET 2015


> On Feb 12, 2015, at 10:24 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> += duplicates the extend method on lists.
>
> Yes it does, and that sometimes causes confusion when people wonder why
> alist += blist is not *quite* the same as alist = alist + blist.

Actually, that's the primary motivator for += and friends -- to
support in-place operations on mutables. Notably numpy arrays.

> It also
> leads to a quite ugly and unfortunate language wart with tuples:
>
> py> t = ([], None)
> py> t[0] += [1]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'tuple' object does not support item assignment
> py> t
> ([1], None)
>
> Try explaining to novices why this is not a bug.

I'm going to have to think about that a fair bit myself -- so yes,
really confusing.

But the "problem" here is that augmented assignment shouldn't work on
immutables at all.

But then we wouldn't have the too appealing to resist syntactic sugar
for integer incrementing.

But are you saying that augmented assignment was simply a mistake
altogether, and therefore no new use of it should be added at all (
and you'd deprecate it if you could)?

In which case, there's nothing to discuss about this case.

-Chris


More information about the Python-ideas mailing list