8.2. Variable scope#
Interactive page
This is an interactive book page. Press launch button at the top right side.
In a few examples of our functions, we used variables inside functions.
In the first example, inside our function we used variables a
and b
, which we created outside our function:
def print_status():
print("The value of variable a is", a)
print("The value of variable b is", b)
a = 6
b = 4
print_status()
In the second example, we used the “temporary” variable x
inside our function.
def print_status_2(x):
print("The value passed to the function is", x)
a = 1.5
print_status_2(a)
These were two examples of different variable “scope”. In computer programming, scope defines the rules that Python uses when it tries to look up the value of a variable.
In the slightly simplified picture we will work with here, variables can have two different types of scopes: global scope and local scope.
If Python looks for a variable value, it first looks in the local scope (also called “local namespace”). If it does not find it, Python will go up into the global scope (also called the “global namespace”) and look for the variable there. If it does not find the varible there either, it will trigger an error (a NameError
, to be precise).
How do we create a global variable? By default, if you create a variable directly in your Python script (and not within a function), it will always be global. So, actually, you’ve already created a bunch of global variables!
Any variables you define inside a function in your code will be local variables (including the input variables automatically created if your function takes any arguments).
If you want to create a global variable inside a function, or make sure the variable you are referring to is the global variable and not the local one, you can do this by the global
qualifier, which we will look at in a minute.
Let’s take a look at this in more detail by analysing a few examples.
8.2.1. Example 1#
Here we’re accessing a global variable inside a function.
a1 = 5
def my_func():
print(a1)
my_func()
a1 = 6
my_func()
In this example, when Python is inside the function my_func()
, it first looks to see if there is a variable a1
in the local scope of the function. It does not find one, so it then goes and looks in the global scope. There, it finds a variable a1
, and so it uses it.
8.2.2. Example 2#
This is an example that doesn’t work.
def my_func():
print(b1)
my_func()
This code gives a NameError
because no variable b1
is created in the global scope. If we run the following code cell and try the code above again, it will work.
b1 = 6
Here you can see one of risks of languages like Python: because of the persistent memory of the kernel, code can succeed or fail depending on what code you have run before it.
If you want to see the error message above again, you can delete variable b1
using this code and run it again:
del b1
8.2.3. Example 3#
Variables defined in the local scope of a function are not accessible outside the function.
def my_func():
x = 5
my_func()
print(x)
8.2.4. Example 4#
Variables passed to functions generally cannot be modified by the function.
def my_func(a):
a = 6
a = 5
my_func(a)
print(a)
This one is a bit subtle (or mega-confusing) because we reuse the same name a
for the local variable in the function as the global variable outside of the function. However, the operation is quite logical. When the function code starts running, it creates a local variable a
to store the value it received. And now, because there is already a local variable called a
, using a
in the function refers to the local variable a
, not the global variable a
we define before calling the function.
Try to add print(a)
as the second line of code in my_func
of Example 4 and rerun the code. What do you observe?
8.2.5. Example 5#
This is a tricky one.
a = 6
def my_func():
a = 7
print(a)
my_func()
print(a)
It would seem that the function refers to the global variable a
and, therefore, changes its value. However, it is tricky since we first use a
in the function in an assignment. An assignment in Python will automatically create a variable if it does not exist, and so Python creates a new variable named a
in the local scope. The name a
inside the function now refers to this newly created local variable, and therefore the global variable will not be changed. In fact, this guarantees that you cannot change global variables inside a function, unless you use the global
qualifier shown in the next example.
Try to add print(a)
as the second line of code in my_func
of Example 5 and rerun the code. What do you observe?
8.2.6. Example 6#
If you want to make sure that the a
inside your function is referring to the global variable a
, you can include the line global a
inside your function to tell Python that you mean the global variable a
.
a = 6
def my_func():
global a
a = 7
print(a)
my_func()
print(a)
Note that, in general, it is considered bad programming practice to use (too many) global variables. Why? When you write longer and bigger sections of code, it is easier to understand what is going on in your function if your function uses only local variables and communicates back and forth using input parameters and return variables. Using too many global variables in a function can be confusing because they are defined in a different place in your code and so you don’t have good oversight of them (bigger projects can easily have 10,000+ lines of code).
In computer science, this is a topic of often intense debate (resulting in a flame war), with global variables being branded as “dangerous”, like in this Stack Exchange post.
We agree with the comments in this post that state that “global variables have their place but should be used sparingly”.
8.2.7. Summary of the rules#
Summary of the rules for global and local variables:
If a local variable of the same name exists or is created by Python (by assignment, for example), then Python uses the local variable.
If you try to use a variable name that does not exist locally, Python checks for a global variable of the same name.
If you want to change the value of a global variable inside a function, then you must use the
global
statement to make it clear to Python than you want the name to refer to the global variable.
import micropip
await micropip.install("jupyterquiz")
from jupyterquiz import display_quiz
import json
with open("questions4.json", "r") as file:
questions=json.load(file)
display_quiz(questions, border_radius=0)