The docstring for collections.defaultdict
is pretty confusing at first read:
defaultdict(default_factory[, ...]) --> dict with default factory
The default factory is called without arguments to produce a new value
when a key is not present, in getitem only. A defaultdict compares
equal to a dict with the same items. All remaining arguments are
treated the same as if they were passed to the dict constructor,
including keyword arguments.
All this is trying to say is that the default "value" has to be a function, and that that function will be called without arguments whenever it's needed (i.e. whenever a normal dict
would raise a KeyError
.) The docstring refers to this function as default_factory
.
The bit with [, ...]
says that you can optionally pass in an object (or keywords) to use as the defaultdict
's initial dictionary. Thus:
In [26]: x = defaultdict(lambda: 0, a=1, b=2)
In [28]: dict(x)
Out[28]: {'a': 1, 'b': 2}
In [29]: x['c'] # This would normally raise a `KeyError`
Out[29]: 0
In [30]: dict(x) # But here it just adds a new key!
Out[30]: {'a': 1, 'b': 2, 'c': 0}
Any function which returns zero when called with no arguments can be used:
In [31]: int()
Out[31]: 0
Thus, as per the counting example from the docs you could create a defaultdict
with a default value of zero with:
defaultdict(int)