[Python-ideas] dict.merge(d1, d2, ...) (Counter proposal for PEP 584)

Guido van Rossum guido at python.org
Thu Mar 21 12:11:18 EDT 2019


On Thu, Mar 21, 2019 at 7:45 AM Antoine Pitrou <solipsis at pitrou.net> wrote:


> On Tue, 5 Mar 2019 16:39:40 +0900
> INADA Naoki <songofacandy at 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)
>
> One should also be able to write `d = dict.merge(d1, d2, ...)`
>
> If dict merging is important enough to get a new spelling, then I think
> this proposal is the best: explicit, unambiguous, immediately
> understandable and easy to remember.
>

I don't find it easy to understand or remember that d1.update(d2) modifies
d1 in place, while d1.merge(d2) first copies d1.

Maybe the name can indicate the copying stronger? Like we did with sorting:
l.sort() sorts in-place, while sorted(l) returns a sorted copy.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190321/3aa78a06/attachment.html>


More information about the Python-ideas mailing list