Which configuration layer receives a write through ChainMap?
ChainMap resolves lookups in mapping order and directs assignments and deletions to the first mapping.
Python example and interpretation
Override a default batch size of eight with sixteen. The view exposes sixteen while the shared defaults remain available to other runs.
from collections import ChainMap
base={'batch':8}
local={}
cfg=ChainMap(local,base)
cfg['batch']=16
[dict(cfg),base]Result [{'batch': 16}, {'batch': 8}]
中文
运行参数覆盖默认配置时,写回会落在哪一层?
ChainMap 依次查找每个映射,写入和删除只作用于第一个映射。
Python 算例与适用条件
默认批次大小为 8,临时改为 16。通过同一视图读取新值,但共享默认表仍为 8,其他运行可继续复用。
from collections import ChainMap
base={'batch':8}
local={}
cfg=ChainMap(local,base)
cfg['batch']=16
[dict(cfg),base]结果 [{'batch': 16}, {'batch': 8}]