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

Andrew Barnert abarnert at yahoo.com
Wed Feb 11 14:44:17 CET 2015


On Feb 10, 2015, at 23:21, Ian Lee <ianlee1521 at gmail.com> wrote:

> I mentioned this on the python-dev list [1] originally as a +1 to someone else suggesting the idea [2]. It also came up in a response to my post that I can't seem to find in the archives, so I've quoted it below [3].
> 
> As the subject says, the idea would be to add a "+" and "+=" operator to dict

There have been other suggestions in the past for a non-mutating equivalent to dict.update, whether it's spelled + or | or a method. I personally think the idea makes sense, but it's probably worth doing a bit of digging to see why those past suggestions failed. (If you're lucky, everyone likes the idea but nobody got around to implementing it, so all you have to do is write a patch and link to the previous discussion. If you're unlucky, someone made a good case that it's an attractive nuisance or a confusing API of something that you won't have an answer for--but at least then you'll know what you need to answer.)

> that would provide the following behavior:
> 
> >>> {'x': 1, 'y': 2} + {'z': 3}
> {'x': 1, 'y': 2, 'z': 3}
> 
> With the only potentially non obvious case I can see then is when there are duplicate keys, in which case the syntax could just be defined that last setter wins,

There's also the issue of what type you get when you add two Mappings of different types. And whether there should be an __radd__ that makes sure a legacy Mapping type plus a dict works. And whether the operator should be added to the Mapping ABC or just to the concrete class (and with a default implementation?). And whether it's worth writing a dict subclass that adds this method and putting it on PyPI as a backport (people writing 3.3+ code or 2.7/3.5 code can then just "from dict35 import dict35 as dict", but of course they still won't be able to add two dicts constructed from literals).

> e.g.:
> 
> >>> {'x': 1, 'y': 2} + {'x': 3}
> {'x': 3, 'y': 2}
> 
> Which is analogous to the example:
> 
> >>> new_dict = dict1.copy()
> >>> new_dict.update(dict2)
> 
> With "+=" then essentially ending up being an alias for ``dict.update(...)``.
> 
> I'd be happy to champion this as a PEP if the feedback / public opinion heads in that direction. 
> 
> 
> [1] https://mail.python.org/pipermail/python-dev/2015-February/138150.html
> [2] https://mail.python.org/pipermail/python-dev/2015-February/138116.html
> [3] John Wong -- 
>> Well looking at just list
>> a + b yields new list
>> a += b yields modified a
>> then there is also .extend in list. etc.  
>> so do we want to follow list's footstep? I like + because + is more natural to read. Maybe this needs to be a separate thread. I am actually amazed to remember dict + dict is not possible... there must be a reason (performance??) for this...
> 
> Cheers,
> 
> ~ Ian Lee
> _______________________________________________
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150211/9d276da5/attachment-0001.html>


More information about the Python-ideas mailing list