collections.defaultdict’s “first argument must be callable”
This page is in community mode. Any user with 100 rep may edit it.
- dictionary
- defaultdict
A defaultdict
won’t work if the desired default value is provided directly:
>>> from collections import defaultdict
>>> dct = defaultdict(float('-inf'))
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
dct = defaultdict(float('-inf'))
TypeError: first argument must be callable
Providing e.g. a lambda
expression instead solves the problem:
>>> dct = defaultdict(lambda: float('-inf'))