I am wondering about the output
sys: maxRecursionDepth = 10
f1, f2, f3, f4, f5, f1,
>>> maxRecursionDepth = 2
# -----------------------------
sys: maxRecursionDepth = 10
f1, f1, f1, f1, f1, f1,
>>> maxRecursionDepth = 6
of code provided below.
What I am wondering about is: What causes that chaining of function calls compared to nesting of function calls has different impact on the by the counter counted calls to the topmost function starting the recursion? In other words, how does it come that nested calls don't reduce the depth of recursion like the chained calls do? All of the nested functions wait for their parameter being evaluated, so they should take space on the stack, but it seems that they don't.
from sys import getrecursionlimit, setrecursionlimit
setrecursionlimit(10)
print(f'sys: maxRecursionDepth = {getrecursionlimit()}')
cnt = 0
def f1():
global cnt
print('f1', end=', ')
cnt += 1
f2()
def f2():
print('f2', end=', ')
f3()
def f3():
print('f3', end=', ')
f4()
def f4():
print('f4', end=', ')
f5()
def f5():
print('f5', end=', ')
f1()
# ---
try:
f1()
except RecursionError:
print(f'\n >>> maxRecursionDepth = {cnt}') # 200
# RecursionError: maximum recursion depth exceeded
print('# -----------------------------')
#"""
from sys import getrecursionlimit, setrecursionlimit
setrecursionlimit(10)
print(f'sys: maxRecursionDepth = {getrecursionlimit()}')
cnt = 0
def f1():
global cnt
print('f1', end=', ')
cnt += 1
f2(f3(f4(f5(f1()))))
def f2(f):
print('f2', end=', ')
f(f3)
def f3(f):
print('f3', end=', ')
f(f4)
def f4(f):
print('f4', end=', ')
f5()
def f5(f):
print('f5', end=', ')
f1()
# ---
try:
f1()
except RecursionError:
print(f'\n >>> maxRecursionDepth = {cnt}') # 996
# RecursionError: maximum recursion depth exceeded