On this page
collections.ChainMap
Up: collections | Next: collections.Counter
Rel: collections.Counter, collections.defaultdict, collections.deque, collections.namedtuple
Ref: docs.python.org — collections.ChainMap
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import ChainMap
Signature & Overview
collections.ChainMap(*maps)
A ChainMap groups multiple dicts (or other mappings) together to create a single, updateable view.
Methods & Attributes
ChainMap.clear(self)— Clear maps[0], leaving maps[1:] intact.ChainMap.copy(self)— New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]ChainMap.fromkeys(iterable, value=None, /)— Create a new ChainMap with keys from iterable and values set to value.ChainMap.get(self, key, default=None)—ChainMap.getmethod/property.ChainMap.items(self)— D.items() -> a set-like object providing a view on D's itemsChainMap.keys(self)— D.keys() -> a set-like object providing a view on D's keysChainMap.new_child(self, m=None, **kwargs)— New ChainMap with a new map followed by all previous maps.ChainMap.parents(...)— New ChainMap from maps[1:].ChainMap.pop(self, key, *args)— Remove key from maps[0] and return its value. Raise KeyError if key not in maps[0].ChainMap.popitem(self)— Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.ChainMap.setdefault(self, key, default=None)— D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in DChainMap.update(self, other=(), /, **kwds)— If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k] If E present and lacks .keys() method, does:ChainMap.values(self)— D.values() -> an object providing a view on D's values
Usage
from collections import ChainMap
defaults = {"host": "localhost", "port": 8080}
overrides = {"port": 9000}
cfg = ChainMap(overrides, defaults)
assert cfg["port"] == 9000 and cfg["host"] == "localhost"
child = cfg.new_child({"debug": True})
assert child["debug"] is True and "debug" not in cfg
Gotchas
- Writes only touch the first mapping:
cfg[k] = vanddel cfg[k]mutatecfg.maps[0]only (deleting a key only present in a fallback raisesKeyError). - Live view, not a copy: Mutating any underlying dict immediately reflects in the
ChainMap; usedict(cfg)if you need a merged snapshot.
REPL Inspect
from collections import ChainMap
help(ChainMap)
Linked from
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/collections/Counter
- tuts/python3/stdlib_module/collections/defaultdict
- tuts/python3/stdlib_module/collections/deque
- tuts/python3/stdlib_module/collections/namedtuple
- tuts/python3/stdlib_module/collections/OrderedDict
- tuts/python3/stdlib_module/collections/UserDict
- tuts/python3/stdlib_module/collections/UserList
- tuts/python3/stdlib_module/collections/UserString
Related notes
- tuts/python3/stdlib_module/collections/Counter Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/OrderedDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserDict links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserList links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString links to this page; shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib