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

Serhiy Storchaka storchaka at gmail.com
Thu Feb 28 02:16:55 EST 2019


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?



More information about the Python-ideas mailing list