[Python-ideas] Adding dictionary merge operator(s) [was: Allow using ** twice]
Guido van Rossum
guido at python.org
Sat Jun 8 08:17:39 CEST 2013
Actually the C API recognizes exactly two variations. See PyDict_Merge in
http://docs.python.org/3/c-api/dict.html.
On Friday, June 7, 2013, Stephen J. Turnbull wrote:
> Updating the topic.
>
> Masklinn writes:
>
> > One of the rather annoying things in dict.update is that it alters the
> > caller in place,
>
> If it didn't, it wouldn't be "update", it would be "merge".
>
> > so it can't be used to merge multiple dicts in an expression.
>
> The fundamental problem is that "dict.merge" is not a well-defined
> operation. Instead, it's more like image composition, which is a
> family of trinary operators (something like 26 of them), combining a
> target, source, and alpha mask in various ways.
>
> "Update" presumably is the most often used operator, but once you get
> into *any* variations from that, there are lots of plausible ones.
>
> > > The "first found wins" interpretation could use a different method:
> > >
> > > {}.extend(d1, d2, …)
> >
> > IIRC in underscore.js this is called "defaults", d1, d2, … are a
> > "stack" of applicable defaults, and thus are merged into the
> > subject if and only if the corresponding keys are missing from
> > the subject.
>
> The "stack of defaults" interpretation of the list seems natural to
> me, but it's not obvious to me whether the list *constructs* the stack
> by pushing each argument in turn (ie, di is consulted in R2L order),
> or *is* the stack (so each di is consulted in L2R order). I guess I
> would use "fallback", so that
>
> d0.fallback(d1, d2, ...)
>
> reads "start with d0, fall back to d1 for missing keys, fall back to
> d2 for still missing keys, ...". YMMV.
>
> Then-again-maybe-I've-just-been-programming-Lisp-for-too-long-ly y'rs,
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org <javascript:;>
> http://mail.python.org/mailman/listinfo/python-ideas
>
--
--Guido van Rossum (on iPad)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130607/765cef9e/attachment-0001.html>
More information about the Python-ideas
mailing list