<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div>I agree so much on your opinion that I was just to create a topic about this if you didn't.</div><div>I also propose here a small modification to make it more general which adds an argument `how` (name to be discussed), telling how to merge the dicts, as many have pointed out that there could be different ways to merge dicts.</div><div>So things would be like</div><div><br></div><div>    def addition_merge(key, values, exists):</div><div>        """</div><div>        :param key: the key to merge</div><div>        :param values: values of dicts to merge indexed at `key`</div><div>        :param exists: whether each dict contains `key`</div><div>        """</div><div>        if any(exists):</div><div>            return True, sum([value for exist, value in zip(exists, values) if exist])</div><div>        else:</div><div>            return False</div><div>    d1.merge(d2, d3, ..., how=addition_merge)</div><div><br></div><div>We could even have</div><div><br></div><div>    def discard(key, values, exists):</div><div>        return not any(exists[1:]), values[0]</div><div>    d1.merge(d2, how=discard)</div><div><br></div><div>which does the same thing as proposed `d1-d2`.</div><div><br></div><div style="position:relative;zoom:1"></div><div id="divNeteaseMailCard"></div><div>This would make things like</div><div>    <span style="font-family: arial; white-space: pre-wrap;">d = d1.merge(iter_of_pairs)</span></div><div><span style="font-family: arial; white-space: pre-wrap;">    d = d1.merge(key=value)</span></div><div><span style="font-family: arial; white-space: pre-wrap;">not working, but people could easily wrap a `dict()` over the iterator or key-value stuff and attach no complication.</span></div><div><br></div><pre><br>At 2019-03-05 15:39:40, "INADA Naoki" <songofacandy@gmail.com> wrote:
>I think some people in favor of PEP 584 just want
>single expression for merging dicts without in-place update.
>
>But I feel it's abuse of operator overload.  I think functions
>and methods are better than operator unless the operator
>has good math metaphor, or very frequently used as concatenate
>strings.
>
>This is why function and methods are better:
>
>* Easy to search.
>* Name can describe it's behavior better than abused operator.
>* Simpler lookup behavior. (e.g. subclass and __iadd__)
>
>Then, I propose `dict.merge` method.  It is outer-place version
>of `dict.update`, but accepts multiple dicts.  (dict.update()
>can be updated to accept multiple dicts, but it's not out of scope).
>
>* d = d1.merge(d2)  # d = d1.copy(); d.update(d2)
>* d = d1.merge(d2, d3)  # d = d1.copy(); d.update(d2); d2.update(d3)
>* d = d1.merge(iter_of_pairs)
>* d = d1.merge(key=value)
>
>
>## Merits of dict.merge() over operator +
>
>* Easy to Google (e.g. "python dict merge").
>* Easy to help(dict.merge). (or dict.merge? in IPython)
>* No inefficiency of d1+d2+d3+...+dN, or sum(list_of_many_dicts)
>* Type of returned value is always same to d1.copy().  No issubclass,
>no __iadd__.
>
>## Why not dict.updated()?
>
>sorted() is a function so it looks different from L.sort()
>But d.updated() is very similar to d.update() for human eyes.
>
>## How about d1 - d2?
>
>If it is really useful, it can be implemented as method too.
>
>dict.discard(sequence_of_keys)
>
>Regards,
>-- 
>INADA Naoki  <songofacandy@gmail.com>
>_______________________________________________
>Python-ideas mailing list
>Python-ideas@python.org
>https://mail.python.org/mailman/listinfo/python-ideas
>Code of Conduct: http://python.org/psf/codeofconduct/
</pre></div><br><br><span title="neteasefooter"><p> </p></span>