Here, we are using set as the missing value default, how can we use an undefined set for that purpose?
Asked
Active
Viewed 20 times
0
-
What do you mean by "undefined"? – user2390182 Jul 07 '23 at 14:38
-
1What is provided the defaultdict constructor is a *factory*, a no-argument callable that -- when called -- produces new independent default values. – user2390182 Jul 07 '23 at 14:41
1 Answers
1
defaultdict
doesn't take a value, it takes a callable thing that it can call (with no arguments) to construct the value when it needs to. Typically a constructor of a class, or a function, like lambda: 'Hello'
.
Each new value will in this case be a set()
, which is an empty set.
>>> d = collections.defaultdict(set)
>>> print(d['new_key'])
set()
Your datastructure is a dictionary of keys to sets, with an empty set as the default value.

Mikael Öhman
- 2,294
- 15
- 21
-
2Well, doesn't have to be constructor. Any no-arg callable will do. – user2390182 Jul 07 '23 at 14:42
-
Good point. Quite common to have a lambda in there so i updated the text. – Mikael Öhman Jul 07 '23 at 14:47
-
Also, `set` *has* a constructor (`__new__`, though you probably meant `__init__`); it *is* a type. – chepner Jul 07 '23 at 16:07