Returning from a multiple stacked call at once
Tim Chase
python.list at tim.thechases.com
Sat Dec 12 11:37:50 EST 2020
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
More information about the Python-list
mailing list