[Python-ideas] adding dictionaries
Alexander Heger
python at 2sn.net
Tue Jul 29 01:18:37 CEST 2014
>> args0 = dict(...)
>> args1 = dict(...)
>>
>> def f(**kwargs):
>> g(**(arg0 | kwargs | args1))
>>
>> currently I have to write
>>
>> args = dict(...)
>> def f(**kwargs):
>> temp_args = dict(dic0)
>> temp_args.update(kwargs)
>> temp_args.update(dic1)
>> g(**temp_args)
>
> The first part of this one of the use cases for functools.partial(), so it
> isn't a compelling argument for easy dict merging. The above is largely an
> awkward way of spelling:
>
> import functools
> f = functools.partial(g, **...)
>
> The one difference is to also silently *override* some of the explicitly
> passed arguments, but that part's downright user hostile and shouldn't be
> encouraged.
yes, poor example due to briefly. ;-)
In my case f would actually do something with the values of kwargs
before calling g, and args1 many not be static outside f.
(hence partial is not a solution for the full application)
def f(**kwargs):
# do something with kwrags, create dict0 and dict1 using kwargs
temp_args = dict(dict0)
temp_args.update(kwargs)
temp_args.update(dict1)
g(**temp_args)
# more uses of dict0
which could be
def f(**kwargs):
# do something with kwargs, create dict0 and dict1 using kwargs
g(**collections.ChainMap(dict1, kwargs, dict0))
# more uses of dict0
Maybe good enough for that case, like with + or |, one still need to
know/learn the lookup order for key replacement, and it is sort of
bulky.
-Alexander
More information about the Python-ideas
mailing list