Nested loop limit?

Peter Hansen peter at engcorp.com
Wed Jul 7 19:17:38 EDT 2004


Nelson Minar wrote:

> Why does CPython have a limit of 21 nested blocks?
> 
> I'm never going to write code this deeply nested by hand, but I could
> imagine writing a program that does. It's also sort of a weird limit.

Is the fact that the limit is actually 20 less weird?  (The 21 was
where "n" was after it raised an exception, not the last legal
amount of indentation.)

And I think the answer is probably something like "CPython,
being written in C, tends to have certain hard-coded limits
much like C programs always do, rather than being nice and
flexible like programs written with more sophisticated languages
like, say, Python."  :-)

> Peter Hansen <peter at engcorp.com> writes:
> 
>> >>> for n in range(100):
>>...   exec '\n'.join([(' ' * i) + 'for i%s in range(2):' % i for i in
>>range(n)])
>>  + '\n' + ' ' * n + 'pass\n'
>>...
>>Traceback (most recent call last):
>>   File "<stdin>", line 2, in ?
>>SystemError: too many statically nested blocks
>> >>> print n
>>21
>>
>>Yes. :-)
>>
>>-Peter
> 
> 



More information about the Python-list mailing list