I'm having trouble understanding the following:
import sys
def f(n):
try:
print('try', n)
f(n + 1)
except:
print('except', n)
if n > 0:
raise
sys.setrecursionlimit(1000)
f(0)
It's output is:
try 0
try 1
try 2
...
try 995
except 996
except 995
...
except 0
I understand that the try block would iterate and print n until the systems recursion limit is met, in which case the except block is entered. My question is, in the except block, why is n decreasing and where is recursion occurring here? Furthermore, why does n not meet the recursion limit precisely? That is, why does the sequence not reach try 1000? Thanks!