# 链map
from collections import ChainMap
a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}
c = ChainMap(a, b)
print(c['x'])
print(c['y'])
print(c['z']) # 3 重复的会使用第一个,修改删除都是只针对第一个
print(len(c)) # 3
print(list(c.keys())) # ['y', 'z', 'x']
print(list(c.values())) # [2, 3, 1]
c['z'] = 10
c['w'] = 40
del c['x']
print(a) # {'z': 10, 'w': 40}
print(c) # ChainMap({'z': 10, 'w': 40}, {'y': 2, 'z': 4})
# del c['y'] # KeyError: "Key not found in the first mapping: 'y'"
values = ChainMap()
values['x'] = 1
values = values.new_child()
values['x'] = 2
values = values.new_child()
values['x'] = 3
print(values) # ChainMap({'x': 3}, {'x': 2}, {'x': 1})
print(values.parents) # ChainMap({'x': 2}, {'x': 1})
print(values.parents.parents) # ChainMap({'x': 1})