Trying to plenny understand when a variable is treated as a "local variable" of a function, I made this toy example:
x = 100
def foo():
temp = x + 1
print(temp)
#x = temp # uncommenting this line will rise an error
foo()
If you keep x = temp
commented, x
will be a global variable, and the code will run ok. But, uncommenting that, x
will be automatically treated as a local varialbe since the beginning of the function, althoug the write instruction is at the end (and despite that Python is an interpeter).
So is it correct?: "A variable is local of a function just if it's written anywhere inside the function?"