[Python-ideas] adding dictionaries

Steven D'Aprano steve at pearwood.info
Tue Jul 29 16:36:05 CEST 2014


On Tue, Jul 29, 2014 at 07:22:34AM +0100, Paul Moore wrote:
> On 29 July 2014 00:04, Alexander Heger <python at 2sn.net> wrote:
> > D = A | B | C
> >
> > becomes
> >
> > D = dict(collections.ChainMap(C, B, A))
> 
> This immediately explains the key problem with this proposal. It never
> even *occurred* to me that anyone would expect C to take priority over
> A in the operator form. But the ChainMap form makes it immediately
> clear to me that this is the intent.

Hmmm. Funny you say that, because to me that is a major disadvantage of 
the ChainMap form: you have to write the arguments in reverse order.

Suppose that we want to start with a, then override it with b, then 
override that with c. Since a is the start (the root, the base), we 
start with a, something like this:

d = {}
d.update(a)
d.update(b)
d.update(c)

If update was chainable as it would be in Ruby:

d.update(a).update(b).update(c)

or even:

d.update(a, b, c)

This nicely leads us to d = a+b+c (assuming we agree that + meaning 
merge is the spelling we want).

The ChainMap, on the other hand, works backwards from this perspective: 
the last dict to be merged has to be given first:

ChainMap(c, b, a)


-- 
Steven


More information about the Python-ideas mailing list