August 2018
Intermediate to advanced
366 pages
10h 14m
English
Another great feature of ChainMap is that it allows updating too, but instead of updating the dictionary where it found the key, it always updates the first dictionary. The result is the same, as on next lookup of that key, we would have the first dictionary override any other value for that key (as it's the first place where the key is checked). The advantage is that if we provide an empty dictionary as the first mapping provided to ChainMap, we can change those values without touching the original container:
>>> population=dict(italy=60, japan=127, uk=65) >>> changes = dict() >>> editablepop = ChainMap(changes, population) >>> print(editablepop['japan']) 127 >>> editablepop['japan'] += 1 >>> print(editablepop['japan']) 128 ...