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

Andrew Barnert abarnert at yahoo.com
Fri Feb 13 01:06:44 CET 2015


On Thursday, February 12, 2015 1:33 PM, Nathan Schneider <neatnate at gmail.com> wrote:


>A reminder about PEP 448: Additional Unpacking Generalizations (https://www.python.org/dev/peps/pep-0448/), which claims that "it 
vastly simplifies types of 'addition' such as combining
dictionaries, and does so in an unambiguous and well-defined way". The 
spelling for combining two dicts would be: {**d1, **d2}


I like that this makes all of the bikeshedding questions obvious: it's a dict display, so the result is clearly a dict rather than a type(d1), it's clearly going to follow the same ordering rules for duplicate keys as a dict display (d2 beats d1), and so on.

And it's nice that it's just a special case of a more general improvement.

However, it is more verbose (and more full of symbols), and it doesn't give you an obvious way to, say, merge two OrderedDicts into an OrderedDict.

Also, code that adds non-literal dicts together can be backported just by adding a trivial dict subclass, but code that uses PEP 448 can't be.


More information about the Python-ideas mailing list