Implementing ChainMap
ChainMap is a dictionary-like class, used to create a single view of multiple mappings. It allows for quick linking between multiple mappings so they can all be considered as a single unit, which is useful when simulating nested scopes and when templating. This can be faster than creating a new dictionary and running update()
calls repeatedly.
The command to create a ChainMap
is as follows:
collections.ChainMap(*maps)
As usual, the *maps
is simply a number of dictionaries or other map objects passed in to be combined into a single, updateable view. If no mappings are passed in, then an empty dictionary is created so the new chain has at least one mapping available to it.
The mappings themselves are contained, behind the scenes, within a list. The list is a public object and it can be accessed or updated via the maps
attribute. When looking for a key, the search occurs over the mapping list until the key is found. However, modifications to the list occur only on the first...