Skip to content

ADR 0071: collections.defaultdict, a dict that holds its default

Status: Final Type: Feature Created: 2026-11-05 Amends: 0030 (a dict may hold a default factory, and d[k] consults it before raising) Related-ADRs: 0059 (the standard library, compiled where CPython is reproduced exactly), 0004 (A190: an int never enters a float slot)

Context

mastermind2 counts outcomes with b: defaultdict[int, int] = defaultdict(int) and b[compare(play, poss)] += 1; it is the program's last refusal. defaultdict is common in Python generally: it is dict with one change, __missing__, which d[k] calls when k is absent; it inserts factory() under k and returns it.

mypy treats defaultdict[K, V] as a subtype of dict[K, V], so a typed program may pass one where a dict is declared, and CPython still calls __missing__ there: the default follows the object. A compiler that knew a dict was a defaultdict only from the slot it is in would raise KeyError inside such a function where CPython inserts the default.

Decision

  1. The default lives in the runtime's dict. The dict object gains a factory code, zero for a plain dict. Every d[k] that misses asks the runtime to insert the default, in generated code on the path that raised KeyError before, and raises only when the dict has no factory. A plain dict costs one call on its miss path, which already ended in a raise; its hit path is unchanged.
  2. The factories. defaultdict(int), defaultdict(float), defaultdict(bool) and defaultdict(str), each the builtin, for a value type it fits: int for an int value (a float one is refused: the default 0 is an int, which CPython would print as 0, A190); float for a float; bool for a bool; str for a str. The construction needs the annotation in scope (b: defaultdict[int, int] = defaultdict(int)), as {} does, and mypy asks for the same one. Not yet: a container factory (list, set, dict, whose default is a new value of the declared member types), a function of the module as the factory (the runtime would call compiled code and pass its exception back), a lambda, a second argument, and an instance key whose class defines __hash__ (its lookup is the probe of A168, which has no miss helper).
  3. What consults it. Only d[k], as in CPython: get, in, pop, setdefault, iteration and len never call __missing__. d[k] op= v reads through d[k], so it inserts the default first, as CPython's does.
  4. What keeps it. d.copy(), copy.copy(d) and copy.deepcopy(d) keep the factory, as CPython's do (dict(d), which would drop it, is not lowered for any dict, nor is == between dicts).
  5. How it prints. print(d), str(d), repr(d) and an f-string write defaultdict(<class 'int'>, {1: 2}), CPython's text for a builtin factory, from the factory the runtime holds, so a defaultdict passed as a dict prints as one.
  6. isinstance(d, defaultdict) is refused, since the answer is the runtime's and this compiler's isinstance answers statically.

Consequences

  • mastermind2's last refusal goes. Every program that reads a dict by d[k] emits one more call on that read's miss path; the census and the speed index measure it.
  • A function factory waits for a program that needs it, and so does a container default; the factory code has room for both.