I know about nonlocal keyword and how to fix this error but I am getting a behaviour that I am not understanding in the code below. If you run this, there is no problem
def test( root):
x=2
def f2(r):
print("***")
x=r #Adding this line is ok
print("----",x)
f2(root)
return x
test(4)
Now try changing x=r to be be the last line in f2 does not work as below
def test( root):
x=2
def f2(r):
print("***")
print("----",x)
x=r #Gives an error "local variable 'x' referenced before assignment"
f2(root)
return x
test(4)
Thanks