[Python-ideas] Allow using ** twice

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jun 6 17:05:08 CEST 2013


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))


Oscar


More information about the Python-ideas mailing list