Might someone explain me why this function is not working ? Shouldn't the "nonlocal" statement makes x
understandable in g
, and therefore in h
?
def f():
def g():
nonlocal x
x= 1
def h():
print(x)
>>> SyntaxError: no binding for nonlocal 'x' found
Edit : I used nonlocal in order not to define x
anywhere else than in g()
: I have to define several variables in my code, and want to do it in a function init_var()
. For lisibility, I want to avoid declaring them in my main function. Is there a way to adapt the previous code for this aim ?