[Python-ideas] dict.merge(d1, d2, ...) (Counter proposal for PEP 584)
songofacandy at gmail.com
Tue Mar 5 04:04:40 EST 2019
On Tue, Mar 5, 2019 at 5:50 PM Nathaniel Smith <njs at pobox.com> wrote:
> On Mon, Mar 4, 2019 at 11:41 PM INADA Naoki <songofacandy at gmail.com> wrote:
> > 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)
> Another similar option would be to extend the dict constructor to
> allow: d = dict(d1, d2, d3, ...)
> Nathaniel J. Smith -- https://vorpus.org
Yes, it's an option too.
One obvious merit of d.merge(...) is it returns same type of d.
`type(d1)(d1, d2)` looks ugly.
But people just want dict instead of some subtype of dict.
This merit is not so important.
I'm bit nervous about adding much overload to constructor.
That's main reason why I proposed method instead of constructor.
INADA Naoki <songofacandy at gmail.com>
More information about the Python-ideas