[Python] Why does Python have a maximum recursion depth?

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    Python has a maximum recursion depth, but no maximum iteration depth. Why is recursion restricted? Wouldn't it be more natural to treat recursion like iteration, and not restrict the number of recursive calls?

    Let me just say that the source of this issue came from trying to implement a stream (see this question for more details about streams). For example, say we want to write a stream to produce the natural numbers:

    def stream_accum(s, n): # force the stream to a list of length n
    def loop(s, acc):
    if len(acc) == n:
    return acc
    hd, tl = s()
    return loop(tl, acc + [hd])
    return loop(s, [])


    def nats():
    def loop(n):
    return n, lambda: loop(n+1)
    return loop(1)


    The recursive definition of streams is quite appealing. However, I guess the better/more pythonic approach would be to use generators.

    Continue reading...

Compartilhe esta Página