I am curious about how Python interpreter handles function.
The code below will generate an error at the line print(x)
.
def foo():
print(x)
x = 1
x = 0
foo()
I would assume that if we expand the code, it will be something like below
x = 0
print(x)
x = 1
I understand the variable scope in Python.
But my real question is, at the line print(x)
, shouldn't the Python interpreter have no knowledge about the later variable declaration and simply reference the x in the global scope?