The collections.defaultdict class provides a dictionary-like structure that calls a factory function to supply missing values. This
factory function (like list, int, or a lambda) is specified during initialization.
Crucially, the defaultdict constructor signature requires the default_factory as its first positional
argument. Any subsequent positional or keyword arguments are used to initialize the contents of the dictionary. This mirrors the behavior of
the standard dict constructor.
Providing the factory using the keyword default_factory=…, as in defaultdict(default_factory=list), is therefore
incorrect and leads to unexpected behavior:
- It does not set the default factory for missing keys. The
defaultdict behaves like a regular dict in
this regard and will raise a KeyError when a missing key is accessed.
- It does initialize the dictionary with a single key-value pair:
{'default_factory': list}.