Why does Python have a limit on the number of static blocks that can be nested?

前端 未结 3 1342
[愿得一人]
[愿得一人] 2020-11-30 03:22

The number of statically nested blocks in Python is limited to 20. That is, nesting 19 for loops will be fine (although excessively time consuming; O(n^19

3条回答
  •  孤城傲影
    2020-11-30 04:22

    See answer here: too many statically nested blocks python You cannot increase it as it is builtin to the python syntax. The limit applies to any kind of code stack (exceptions, loops, etc.) and is a decision by the designers (presumably to keep memory usage reasonable). One weird thing is that here: https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Include/code.h#L95 it says 20 is the max number in a function. But I just tried nesting 23 for loops, not inside a function, and you still get the error.

提交回复
热议问题