From another question, I see how to create a defaultdict of defaultdict of defaultdict... as:
Working form | Using it | Output |
---|---|---|
|
|
|
It works as desired, but I'm having trouble understanding it.
Also, why does the following not work at all:
Non Working form | Using it | Output |
---|---|---|
|
|
|
Can someone explain, how tree = lambda: defaultdict(tree)
works and why tree = defaultdict(lambda: tree)
doesn't work?
EDIT: @Samwise notes in the comments that tree
and each argument to defaultdict
need to be callable, so to make the second form work, it'd need to be:
tree = lambda: defaultdict(lambda: tree())
but since lambda: tree()
is equivalent to tree
, this revised form is equivalent to the first form.
Here:
tree = lambda: defaultdict(tree)
tree
is a function, and each time it is executed, it creates a defaultdict. The default values of the defaultdict are given by calling tree
again, which creates a new defaultdict each time.
Here:
tree = defaultdict(lambda: tree)
tree
is one specific defaultdict. The default values of the defaultdict are given by a function that returns tree
, the same specific defaultdict. So the default values of the dictionary are itself.