The below provided code shows for me unexpected and strange behavior. With not out-commented print statement the final value of the counter variable differs from the value with out-commented print statement. How can an inserted print() impact the final value of the counter 'cnt'?
Here the output with the print statement printing:
sys: maxRecursionDepth = 10
> f(s) 0
>>> f(s) 1
> f(s) 1
>>> f(s) 2
f() maxRecursionDepth = 2
And here the output with the out-commented print:
sys: maxRecursionDepth = 10
>>> f(s) 1
>>> f(s) 2
f() maxRecursionDepth = 3
And here the code which shows this for me strange effect I can't explain how it comes:
from sys import getrecursionlimit, setrecursionlimit
setrecursionlimit(10)
print(f'sys: maxRecursionDepth = {getrecursionlimit()}')
cnt = 0
def f(s):
global cnt
#print(' >', s, cnt) # <<< CHANGES the final value of 'cnt' !!!
cnt += 1
print('>>>', s, cnt)
eval(s)
# ---
try:
f("f(s)")
except RecursionError:
print(f'f() maxRecursionDepth = {cnt}')
# RecursionError: maximum recursion depth exceeded