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

Carl Meyer carl at oddbird.net
Fri Feb 13 00:38:24 CET 2015


On 02/12/2015 04:29 PM, Donald Stufft wrote:
> 
>> On Feb 12, 2015, at 6:28 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
>>
>> On Thu, Feb 12, 2015 at 5:25 AM, Donald Stufft <donald at stufft.io> wrote:
>>> I’ve wanted this several times, explicitly the copying variant of it. I always
>>> get slightly annoyed whenever I have to manually spell out the copy and the
>>> update.
>>
>> copy-and-update:
>>
>>  dict(old_dict, **other_dict)
>>
> 
> Only works if other_dict’s keys are all valid keyword arguments and AFAIK is considered an implementation detail of CPython.

To be clear, kwargs to the dict() constructor are not an implementation
detail of CPython. The implementation detail of CPython (2.x only) is
that this technique works at all if other_dict has keys which are not
strings. That has been changed for consistency in Python 3, and never
worked in any of the alternative Python implementations AFAIK. So you're
right that this technique is not usable as a general-purpose
copy-and-update.

Also, Guido doesn't like it:
http://mail.python.org/pipermail/python-dev/2010-April/099459.html

I think the fact that it is so often recommended is another bit of
evidence that there is demand for copy-and-update-as-expression, though.

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150212/0989621d/attachment.sig>


More information about the Python-ideas mailing list