Why does the following work:
def rec(a, b, c):
global nmb_calls
nmb_calls += 1
# other stuff
rec(...)
p_list = []
nmb_calls = 0
rec(a=p_list, b, c)
print(p_list)
but something like:
def rec(a, b, c):
nonlocal nmb_calls
nmb_calls += 1
# other stuff
rec(...)
def call_rec(usr_b):
p_list = []
nmb_calls = 0
rec(a=p_list, b=usr_b, c)
return p_list
fail with the error: SyntaxError: no binding for nonlocal 'nmb_calls' found
I thought nonlocal means that rec would look up in the enclosing scope (that of call_rec), find nmb_calls and use that?
nmb_callsis aglobalvariable but not in the second one.call_recis NOT an enclosing scope ofrec. You have to actually nest a function inside another to achieve that.