How safe is recursion in Python?

后端 未结 5 2053
青春惊慌失措
青春惊慌失措 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 06:11

    Python limits the depth of recursion to a value you can find out by calling sys.getrecursionlimit and change by calling sys.setrecursionlimit. The default value isn't terribly big. If you set it too high, then you can end up blowing out the C stack when Python recurses. How high "too high" is is platform-dependent, though the default value should be safe (you get a Python exception rather than a crash) on all platforms.

    There are languages that make strong guarantees about tail-call optimization. Scheme is the best-known example; it is a Lisp dialect. You should consider learning at least one Lisp-like language no matter how much you may dislike your professor.

提交回复
热议问题