[Python-ideas] Dict joining using + and +=
Brandt Bucher
brandtbucher at gmail.com
Wed Feb 27 15:18:06 EST 2019
I’d like to try my hand at implementing this, if nobody else is interested. I should be able to have something up today.
Brandt
> On Feb 27, 2019, at 11:05, João Matos <jcrmatos at gmail.com> wrote:
>
> Hello,
>
> Great.
> Because I don't program in any other language except Python, I can't make the PR (with the C code).
> Maybe someone who program in C can help?
>
> Best regards,
>
> João Matos
>> On 27-02-2019 18:48, Guido van Rossum wrote:
>>
>>> On Wed, Feb 27, 2019 at 10:42 AM Michael Selik <mike at selik.org> wrote:
>>>
>>>
>>>> On Wed, Feb 27, 2019 at 10:22 AM Anders Hovmöller <boxed at killingar.net> wrote:
>>>> I dislike the asymmetry with sets:
>>>>
>>>> > {1} | {2}
>>>> {1, 2}
>>>>
>>>> To me it makes sense that if + works for dict then it should for set too.
>>>>
>>>> / Anders
>>>>
>>>> > On 27 Feb 2019, at 17:25, João Matos <jcrmatos at gmail.com> wrote:
>>>> >
>>>> > Hello,
>>>> >
>>>> > 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
>>>
>>>
>>> The dict subclass collections.Counter overrides the update method for adding values instead of overwriting values.
>>>
>>> https://docs.python.org/3/library/collections.html#collections.Counter.update
>>>
>>> Counter also uses +/__add__ for a similar behavior.
>>>
>>> >>> c = Counter(a=3, b=1)
>>> >>> d = Counter(a=1, b=2)
>>> >>> c + d # add two counters together: c[x] + d[x]
>>> Counter({'a': 4, 'b': 3})
>>>
>>> At first I worried that changing base dict would cause confusion for the subclass, but Counter seems to share the idea that update and + are synonyms.
>>
>> Great, this sounds like a good argument for + over |. The other argument is that | for sets *is* symmetrical, while + is used for other collections where it's not symmetrical. So it sounds like + is a winner here.
>>
>> --
>> --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/157a37ff/attachment-0001.html>
More information about the Python-ideas
mailing list