Hello Jose Ricardo Krauss,
You ask:
“How come the Python interpreter is able to read the variable var
before its declaration in the example 1:”
but this is incorrect, the interpreter doesn’t read the variable before
the variable is created because the function isn’t called and executed,
only defined.
Let us follow the interpreter’s steps here. First step is the interpeter
reaches the “def” statement, and prepares a new function object:
def myFunc():
print("Variable 'var' is", var)
Inside the function, the compiler sees a reference to ‘var’, and it
compiles instructions to look up the global variable ‘var’, but it
doesn’t try to actually look it up yet. If it did, it would fail,
because ‘var’ doesn’t exist yet.
Once the function is compiled and assigned to the name “myFunc”, the
interpreter continues:
var = 1
myFunc()
Here the interpreter assigns the value 1 to the global variable ‘var’
first, then calls ‘myFunc’. So by the time myFunc actually looks
up ‘var’ it has a value assigned.
Now let’s look at your second example:
# Example 2
def myFunc():
print("Variable 'var' is", var)
var = 2
Again, the interpreter sees the “def” statement and begins to prepare a
new function object. But this time, the compiler sees a reference to
‘var’, but it knows that it is a local variable, not global. How does
it know that? Because Python follows the rule:
- If you assign a value to a variable anywhere inside a function, that
variable is treated as a local variable.
and the compiler sees the line “var = 2”. So it compiles a different set
of instructions to look up the local variable ‘var’ rather than a global
variable ‘var’.
(You can have local variables with the same name as global variables.)
The rest of the process is the same, until it comes to actually calling
myFunc. Inside myFunc, the interpreter tries to look up the value of
‘var’, but it is a local, not a global, and the local var doesn’t have a
value yet – it doesn’t get a value until the next line.