How safe is recursion in Python?

后端 未结 5 2094
青春惊慌失措
青春惊慌失措 2021-01-22 05:37

I\'m working on an AI homework, and despite my professor\'s suggestions, I have no intention of writing this assignment in lisp. However, I do want to write it recursiv

5条回答
  •  难免孤独
    2021-01-22 05:59

    How deep does the Python stack go?

    The default recursion limit in python is 1000 frames. You can use sys.setrecursionlimit(n) to change that at your own risk.

    If you're set on using python, I would suggest using a pattern more suited to the language. If you want to use the recursive style search, and need arbitrary stack depth, you can make use of python's enhanced generators (coroutines) to create a "trampoline pattern" (there an example in PEP342)

    and despite my professor's suggestions, I have no intention of writing this assignment in lisp

    If the exercise is intended to be recursion-based, a tail-call optimized language, like a lisp, is probably your best bet.

提交回复
热议问题