[Python-ideas] adding dictionaries

Alexander Heger python at 2sn.net
Tue Jul 29 01:04:42 CEST 2014


>> args = dict(...)
>> def f(**kwargs):
>>    temp_args = dict(dic0)
>>    temp_args.update(kwargs)
>>    temp_args.update(dic1)
>>    g(**temp_args)
>
> No, you just have to write a one-liner with ChainMap, except in the (very rare) case where you're expecting g to hold onto and later modify its kwargs.

yes, this (modify) is what I do.

In any case, it would still be

g(**collections.ChainMap(dict1, kwargs, dic0))

In either case a new dict is created and passed to g as kwargs.

It's not pretty, but it does work.  Thanks.

so the general case

D = A | B | C

becomes

D = dict(collections.ChainMap(C, B, A))

(someone may suggest dict could have a "chain" constructor class
method D = dict.chain(C, B, A))


More information about the Python-ideas mailing list