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

Chris Barker - NOAA Federal chris.barker at noaa.gov
Sat Feb 14 16:59:58 CET 2015


> dict.update() goes back to Python 1.5 and perhaps older.

Indeed - and not only that, but update() has proved useful, and there
are no methods  for the other options proposed. Nor have they been
proposed to be added, AFAIK.

There is Counter, but as Steven points out -- it's a subclass, partly
because that behavior isn't usable in the general case of dicts with
arbitrary keys.

However, this thread started with a desire for the + and += operators
-- essentially syntactic sugar ( which I happen to like ). So is there
a real desire / use case for easy to spell merge-into-new-dict
behavior at all?

-Chris


> That horse has
> long since bolted. Let's just proclaim the argument about *semantics*
> settled, so we can concentrate on the really important part: arguing
> about the colour of the bike-shed. I mean, syntax/interface.
>
> There are various semantics for merging/updating a mapping with
> another mapping. We've covered them repeatedly, I'm not going to repeat
> them here. But the most commonly used one, the most general one, is what
> dict.update does, and even though it is something that needs to be
> learned, it's pretty easy to learn because it matches the left-to-right
> behaviour of other parts of Python.
>
> So let's just agree that we're looking for a not-in-place equivalent to
> dict.update. Anyone who wants one of the less general versions can do
> what Counter does, and subclass.
>
> Do we have consensus that copy-and-update should use the same semantics
> as dict.update?
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list