Based of this answer, I want to create a one line tree as part of another class like thus:
self._tree = collections.defaultdict(lambda: self._tree)
I will need to allow the users of said class to add path elements to the tree and run some callback starting at the lowest tree level. My naive implementation raises a error when I run pytest
:
def _add(self, tree, path):
for node in path:
tree = tree[node]
def _run(self, tree, callback):
for key in tree.keys():
callback(tree[key]) # !!! Recursion detected (same locals & position)
self._run(key)
This code works iff the tree is defined as
def tree():
return collections.defaultdict(tree)
self._tree = tree()
Why is my naive approach not working with the lambda expression?
⚠ The Zen of Python states that
Simple is better than complex.
The one-line lambda makes the code complex where there is a simpler implementation. Therefore the one-line lambda should not be used in production code. However, I shall leave this question here for academic interest.