3 ms·
The only time I ever do it is for tree walking, and then it's always a private internal function, so like: def process_that_tree(my_tree): def _handl
by mikepurvis 3y ago
The only time I ever do it is for tree walking, and then it's always a private internal function, so like:
def process_that_tree(my_tree):
def _handle(node):
# do the things
for child in node.children:
_handle(child)
_handle(my_tree.root)
The processing can even be brought outside sometimes, or the walker can just become a generator, yielding nodes out to a regular loop doing the actual work. Either way, the recursive part is kept very minimal and easy to reason about.
Obviously for the filesystem these kinds of abstractions exist already in shutil/pathlib/glob, but it still can have a place for dealing with other kinds of hierarchies, like a package dependency tree or the like.