Zahari Dim <> added the comment:

On Sat, Sep 8, 2018 at 1:15 PM Serhiy Storchaka <> wrote:
> 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.

I don't know where the idea that the underlying mappings are an
implementation detail comes from. It certainly isn't from the
documentation, which mentions uses such as nested scopes and
templates, which cannot be attained with a single mapping. It also
doesn't match my personal usage, where as discussed, even the simpler
cases benefit from information on the underlying mappings. It is a
surprising claim to make given than the entirety of the public
interface specific to ChainMap (maps, new_child and parents) deals
with the fact that there is more structure than one mapping. I also
have a hard time discerning this idea from Raymond's messages.

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

This "trivial snatch of code" contains at least two issues that make
it fail in situations where the actual implementation of `__getitem__`
would work, opening the door for hard to diagnose corner cases. If
anything, in my opinion, the fact that this code is being proposed as
an alternative reinforces the idea that the implementation of the
searching method should be in the standard library.


Python tracker <>
Python-bugs-list mailing list

Reply via email to