Paste Search Dynamic
ChainMap
  1. # In a nutshell, this seems search all maps to find a key
  2. # but other ops are limited to the first map.
  3. from collections import ChainMap
  4. m1 = {'c': 2}
  5. m2 = {'a': 0, 'b': 1}
  6. cm = ChainMap(m1, m2)
  7. print(list(cm))
  8. print(cm)
  9. print('b =', cm['b'])
  10. cm['b'] = 7 # Insert m1 via cm
  11. print(cm)
  12. print('b =', cm['b'])
  13. del cm['b'] # Delete m1 via cm
  14. print(cm)
  15. print('b =', cm['b'])
  16. m2['b'] = 7 # Update m2; change visible from cm
  17. print(cm)
  18. print('b =', cm['b'])
  19. try:
  20.         del cm['b'] # Error: m2 not visible from cm for delete
  21. except exception as err:
  22.         print(err)
Parsed in 0.008 seconds