[Python-ideas] Dict joining using + and +=

David Mertz mertz at gnosis.cx
Wed Feb 27 15:11:21 EST 2019


"foo" + "bar" != "bar" + "foo"

On Wed, Feb 27, 2019, 12:35 PM George Castillo <gmcastil at gmail.com> wrote:

> The key conundrum that needs to be solved is what to do for `d1 + d2` when
>> there are overlapping keys. I propose to make d2 win in this case, which is
>> what happens in `d1.update(d2)` anyways. If you want it the other way,
>> simply write `d2 + d1`.
>
>
> This would mean that addition, at least in this particular instance, is
> not a commutative operation.  Are there other places in Python where this
> is the case?
>
> ~ George
>
>
> On Wed, Feb 27, 2019 at 10:06 AM Guido van Rossum <guido at python.org>
> wrote:
>
>> On Wed, Feb 27, 2019 at 8:50 AM Rhodri James <rhodri at kynesim.co.uk>
>> wrote:
>>
>>> On 27/02/2019 16:25, João Matos wrote:
>>> > I would like to propose that instead of using this (applies to Py3.5
>>> and upwards)
>>> > dict_a = {**dict_a, **dict_b}
>>> >
>>> > we could use
>>> > dict_a = dict_a + dict_b
>>> >
>>> > or even better
>>> > dict_a += dict_b
>>>
>>> While I don't object to the idea of concatenating dictionaries, I feel
>>> obliged to point out that this last is currently spelled
>>> dict_a.update(dict_b)
>>>
>>
>> This is likely to be controversial. But I like the idea. After all, we
>> have `list.extend(x)` ~~ `list += x`. The key conundrum that needs to be
>> solved is what to do for `d1 + d2` when there are overlapping keys. I
>> propose to make d2 win in this case, which is what happens in
>> `d1.update(d2)` anyways. If you want it the other way, simply write `d2 +
>> d1`.
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>> _______________________________________________
>> 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/
>>
> _______________________________________________
> 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/20190227/8f49a7d0/attachment.html>


More information about the Python-ideas mailing list