Il 15/12/2020 10:41, jak ha scritto:
Il 12/12/2020 18:20, Dieter Maurer ha scritto:
ast wrote at 2020-12-12 07:39 +0100:
In case a function recursively calls itself many times,
is there a way to return a data immediately without
unstacking all functions ?

Python does not have "long jump"s (out of many functions, many loop
incarnations).
In some cases, you can use exceptions to emulate "long jump"s.


I don't know what you mean by 'emulating' because using the exception handler you simply defer unstacking to it. The real problem is that if you write a function that ends by calling itself and does not take advantage of the branch of code following the recursive call, then it is a wrong way to use a recursive function which should be replaced with a simple one containing a loop where you can insert a break statement wherever you like. IMHO

Greetings.


this could be a way to emulate a long_jump:

def f(i):
    if i < 10:
        i += 1
        yield from f(i)
    else:
        yield i

i = 0
retult = 0
for n in f(i):
    result = n
    break
print(result)

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to