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

James Lu jamtlu at gmail.com
Thu Feb 28 07:40:25 EST 2019


I agree with Storchaka here. The advantage of existing dict merge syntax is that it will cause an error if the object is not a dict or dict-like object, thus preventing people from doing bad things.


> On Feb 28, 2019, at 2:16 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 
> 27.02.19 20:48, Guido van Rossum пише:
>> On Wed, Feb 27, 2019 at 10:42 AM Michael Selik <mike at selik.org <mailto:mike at selik.org>> wrote >     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.
> 
> Counter uses + for a *different* behavior!
> 
> >>> Counter(a=2) + Counter(a=3)
> Counter({'a': 5})
> 
> I do not understand why we discuss a new syntax for dict merging if we already have a syntax for dict merging: {**d1, **d2} (which works with *all* mappings). Is not this contradicts the Zen?
> 
> _______________________________________________
> 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/


More information about the Python-ideas mailing list