|
>>> from collections import ChainMap
>>> inv = ChainMap({'Monopoly': 20, 'Nintendo': 200}, {'iMac': 1000, 'Chromebook': 800, 'PC': 400}, {'Jeans': 40, 'T-Shirt': 10})
>>> inv['iMac'] = 9001
>>> inv
ChainMap({'Monopoly': 20, 'Nintendo': 200, 'iMac': 9001}, {'iMac': 1000, 'PC': 400, 'Chromebook': 800}, {'T-Shirt': 10, 'Jeans': 40})
This strikes me very much as the wrong default behaviour. If you want the (IMO expected) behaviour of updating maps later in the list, the docs advocate creating a entirely new class with overridden set and delete methods[0]. That's not the end of the world, but if they had made this the default behaviour then the getting the current behaviour would just be chain_map.maps[0][key] = value
and you wouldn't need to create a new class at all.Does anyone know why this decision was made? [0]: https://docs.python.org/3.7/library/collections.html#chainma... |
FWIW I find the current behavior preferable. In fact in all cases I use ChainMap the first mapping is always an empty dictionary, because I specifically don't want to mutate the others.