On this page
  1. collections.ChainMap
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

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

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

REPL Inspect

from collections import ChainMap
help(ChainMap)

Linked from

Related notes