Working form
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.
Non-working form
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.