How to do it...

  1. chainmap_import.py is a basic example of how a ChainMap actually operates in use. First, ChainMap is imported, then two dictionaries are created. A ChainMap object is created from the two dictionaries. Finally, the key:value pairs from the ChainMap are printed:

Notice how the ordering of the dictionaries impacts the results that are printed if two keys are the same, since the first mapping is the object that will be searched through first for the desired key.

  1. The following examples come from the Python documentation at https://docs.python.org/3/library/collections.html#collections.ChainMap. chainmap_builtins.py simulates ...

Get Secret Recipes of the Python Ninja now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.