<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 27, 2019 at 11:18 PM Serhiy Storchaka <<a href="mailto:storchaka@gmail.com">storchaka@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">27.02.19 20:48, Guido van Rossum пише:<br>
> <br>
> On Wed, Feb 27, 2019 at 10:42 AM Michael Selik <br>
> <<a href="mailto:mike@selik.org" target="_blank">mike@selik.org</a> <br>
> <mailto:<a href="mailto:mike@selik.org" target="_blank">mike@selik.org</a>>> wrote >Â Â Â The dict subclass collections.Counter overrides the update method<br>
>Â Â Â for adding values instead of overwriting values.<br>
> <br>
>Â Â Â <a href="https://docs.python.org/3/library/collections.html#collections.Counter.update" rel="noreferrer" target="_blank">https://docs.python.org/3/library/collections.html#collections.Counter.update</a><br>
> <br>
>   Counter also uses +/__add__ for a similar behavior.<br>
> <br>
>Â Â Â Â Â >>> c = Counter(a=3, b=1)<br>
>Â Â Â Â Â >>> d = Counter(a=1, b=2)<br>
>Â Â Â Â Â >>> c + d # add two counters together:Â c[x] + d[x]<br>
>Â Â Â Â Â Counter({'a': 4, 'b': 3})<br>
> <br>
>Â Â Â At first I worried that changing base dict would cause confusion for<br>
>   the subclass, but Counter seems to share the idea that update and +<br>
>Â Â Â are synonyms.<br>
> <br>
> <br>
> Great, this sounds like a good argument for + over |. The other argument <br>
> is that | for sets *is* symmetrical, while + is used for other <br>
> collections where it's not symmetrical. So it sounds like + is a winner <br>
> here.<br>
<br>
Counter uses + for a *different* behavior!<br>
<br>
 >>> Counter(a=2) + Counter(a=3)<br>
Counter({'a': 5})<br></blockquote><div><br></div><div>Well, you can see this as a special case. The proposed + operator on Mappings returns a new Mapping whose keys are the union of the keys of the two arguments; the value is the single value for a key that occurs in only one of the arguments, and *somehow* combined for a key that's in both. The way of combining keys is up to the type of Mapping. For dict, the second value wins (not so different as {'a': 1, 'a': 2}, which becomes {'a': 2}). But for other Mappings, the combination can be done differently -- and Counter chooses to add the two values.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I do not understand why we discuss a new syntax for dict merging if we <br>
already have a syntax for dict merging: {**d1, **d2} (which works with <br>
*all* mappings). Is not this contradicts the Zen?<br></blockquote><div><br></div><div>But (as someone else pointed out) {**d1, **d2} always returns a dict, not the type of d1 and d2.</div><div><br></div><div>Also, I'm sorry for PEP 448, but even if you know about **d in simpler contexts, if you were to ask a typical Python user how to combine two dicts into a new one, I doubt many people would think of {**d1, **d2}. I know I myself had forgotten about it when this thread started! If you were to ask a newbie who has learned a few things (e.g. sequence concatenation) they would much more likely guess d1+d2.</div><div><br></div><div>The argument for + over | has been mentioned elsewhere already.</div><div><br><a class="gmail_plusreply" id="plusReplyChip-21" href="mailto:eric@trueblade.com" tabindex="-1">@Eric Smith</a></div><div>> I'd help out.</div><div><br></div><div>Please do! I tried to volunteer Stephen d'Aprano but I think he isn't interested in pushing through a controversial PEP.</div><div><br></div><div>The PEP should probably also propose d1-d2.<br></div></div><br>-- <br><div dir="ltr" class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div></div>