Serhiy Storchaka <> added the comment:

I concur with Raymond. The purpose of ChainMap is providing a mapping that 
hides the implementation detail of using several mappings as fallbacks. If you 
don't want to hide the implementation detail, you don't need to use ChainMap.

ChainMap exposes underlying mappings as the maps attribute, so you can use this 
implementation detail if you know that it is a ChainMap an not a general 
mapping. It is easy to write a code for searching what mapping contains the 
specified key.

    for m in cm.maps:
        if key in m:
            found = m
        # raise an error or set a default,
        # what is appropriate for your concrete case

or even

    found = next(m for m in cm.maps if key in m)

if the key is always found or StopIteration is appropriate.

I don't think that such trivial snatch of code is worth adding a special 
ChainMap method or even documenting it explicitly in the ChainMap documentation.

nosy: +serhiy.storchaka

Python tracker <>
Python-bugs-list mailing list

Reply via email to