On Thu, Mar 21, 2019 at 7:45 AM Antoine Pitrou <[email protected]> wrote:


> On Tue, 5 Mar 2019 16:39:40 +0900
> INADA Naoki <[email protected]>
> 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)
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to