On 2020-12-12 07:39, ast wrote: > In case a function recursively calls itself many times, > is there a way to return a data immediately without > unstacking all functions ?
Not that I'm aware of. If you use recursion (and AFAIK, Python doesn't support tail-recursion), you pay all the pushes & pay all the pops. If you convert it to an iterative algorithm, you can bail early with items still in the work queue: while queue: item = queue.get() if test(item): print(f"Found it, bailing early with {len(queue)} item(s)") break more = process(item) if more: queue.extend(more) -tkc -- https://mail.python.org/mailman/listinfo/python-list