I want to dissolve the recursion of the following function because some input data lead to an exceeding recursion depth error. Increasing the recursion depth is not a solution in the long-run.
def foo(x):
for element in x.elements:
element.depth = x.depth + 1
self.foo(element)
List flattening is not applicable since the original list structure needs to be preserved.
This solution uses a generator, containing a recursion though. Does the fact that it is a generator make a difference?
Finally, there is the stack approach. Here, I am not sure if this is 100% applicable since the interdependence of the to-be-assigned values.
What would be an elegant (Pythonic), non-recursive solution?