1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Does Python optimize tail recursion?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following piece of code which fails with the following error:


    RuntimeError: maximum recursion depth exceeded
    ​

    I attempted to rewrite this to allow for tail call optimization (TCO). I believe that this code would have been successful if a TCO had taken place.

    def trisum(n, csum):
    if n == 0:
    return csum
    else:
    return trisum(n - 1, csum + n)

    print(trisum(1000, 0))


    Should I conclude that Python does not do any type of TCO, or do I just need to define it differently?

    Continue reading...

Compartilhe esta Página