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¶
- 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 raisedKeyErrorbefore, 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. - The factories.
defaultdict(int),defaultdict(float),defaultdict(bool)anddefaultdict(str), each the builtin, for a value type it fits:intfor anintvalue (afloatone is refused: the default0is an int, which CPython would print as0, A190);floatfor afloat;boolfor abool;strfor astr. 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). - What consults it. Only
d[k], as in CPython:get,in,pop,setdefault, iteration andlennever call__missing__.d[k] op= vreads throughd[k], so it inserts the default first, as CPython's does. - What keeps it.
d.copy(),copy.copy(d)andcopy.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). - How it prints.
print(d),str(d),repr(d)and an f-string writedefaultdict(<class 'int'>, {1: 2}), CPython's text for a builtin factory, from the factory the runtime holds, so a defaultdict passed as adictprints as one. isinstance(d, defaultdict)is refused, since the answer is the runtime's and this compiler'sisinstanceanswers 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.