[Python-ideas] Adding dictionary merge operator(s) [was: Allow using ** twice]
Stephen J. Turnbull
stephen at xemacs.org
Sat Jun 8 07:47:43 CEST 2013
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,
More information about the Python-ideas
mailing list