AQA 7517, section 4.1.1.14
A global variable is declared outside every subroutine, and can be accessed from anywhere in the program, including inside subroutines. It exists for as long as the program is running.
This is the opposite of a local variable (4.1.1.13), which exists only inside one subroutine and only while that subroutine runs.
Local and global compared
| Local variable | Global variable | |
|---|---|---|
| Declared | Inside a subroutine | Outside all subroutines |
| Accessible from | Only that subroutine | Anywhere in the program |
| Exists | While the subroutine runs | For the whole run of the program |
| Memory | Freed when the subroutine ends | Kept until the program ends |
| Risk of unexpected changes | Low | High |
Using a global variable
In Python, a subroutine can read a global variable freely. To change it, the subroutine must say global, otherwise Python creates a new local variable with the same name.
score = 0 # global variable
def add_points(n):
global score # refer to the global one
score = score + n
def show_score():
print("Score:", score) # reading needs no keyword
add_points(10)
add_points(5)
show_score() # Score: 15
Without the line global score, the function would fail, because score would be treated as a new local variable that is used before it has a value.
Why globals cause problems
- Hidden dependencies. A subroutine that uses a global works only if that variable exists and holds the right value, so it can't be understood or reused on its own.
- Unexpected changes. Any subroutine can change a global, so if the value is wrong it's difficult to find out which one did it.
- Harder to test. The result depends on the state of the rest of the program, not only on the inputs.
- Name clashes. In a large program, two parts may use the same name by mistake.
- Memory. A global is kept for the whole run, whereas locals are freed.
A better design passes data in through parameters and sends results back through return values. Then the flow of data is visible.
# Better: no global needed
def add_points(score, n):
return score + n
score = 0
score = add_points(score, 10)
score = add_points(score, 5)
print(score) # 15
When are globals acceptable?
Occasionally, a global is a sensible choice:
- values that are needed throughout the program and don't change, such as
MAX_PLAYERSorVAT_RATE(global constants are generally harmless); - data that really is shared by most of the program, such as a game's overall settings.
Even then, many programmers prefer to keep them to a minimum.
Worked example
Task: predict the output.
count = 5
def change():
count = 20
print("In:", count)
change()
print("Out:", count)
The output is:
In: 20
Out: 5
Inside change, the line count = 20 creates a new local variable called count. It doesn't touch the global one, which stays at 5. If the subroutine had started with global count, the output would be In: 20 and Out: 20.
Exam tip: when you contrast local and global, cover scope (where it can be used) and lifetime (how long it exists), and give a reason that global variables are risky, such as "any subroutine can change it, which makes bugs harder to trace".
Check yourself
What is a global variable?
A variable declared outside all subroutines that can be accessed from anywhere in the program for as long as it runs.
Give two differences between a local and a global variable
Any two of: a local variable is declared inside a subroutine but a global one is outside; a local variable can only be used in its subroutine but a global one anywhere; a local variable exists only while the subroutine runs but a global one exists for the whole program.
Why are global variables often considered bad practice?
Any part of the program can change them, which makes bugs harder to find, and subroutines that rely on them can't be reused or tested independently.
How can data be shared between subroutines without using globals?
By passing it in as parameters and sending results back as return values.