[Python-ideas] Allow using ** twice
Zachary Ware
zachary.ware+pyideas at gmail.com
Thu Jun 6 17:08:42 CEST 2013
On Thu, Jun 6, 2013 at 10:05 AM, Oscar Benjamin
<oscar.j.benjamin at gmail.com> wrote:
> On 6 June 2013 15:54, Markus Unterwaditzer <markus at unterwaditzer.net> wrote:
>> This indicates for me that it generally should be possible to generate the union of two dicts with sth like {} + {}.
>
> It cannot be a union since the dicts can have different values
> correspond to the same keys.
>
> However you can make a class that will try several mappings sequentially e.g.:
>
> class MapJoin:
> def __init__(self, *mappings):
> self.mappings = mappings
> def __getitem__(self, key):
> for mapping in self.mappings:
> try:
> return mapping[key]
> except KeyError:
> pass
> else:
> raise KeyError(str(key))
> # Perhaps other mapping methods here...
>
> Then you can do:
>
> func(**MapJoin(map1, map2))
>
That sounds rather like collections.ChainMap.
More information about the Python-ideas
mailing list