Using the global Statement
00:00 You already know that when you try to assign a value to a global name inside a function, you create a new local name in the local scope of the function.
So let’s consider what the output for this code would be. Here you define a variable
counter with the value
0, a function
update_counter() that seems to increase the variable
1, and a function call at the end.
You may think that the program output would be the value
1, right? Let’s try running this and see what happens.
On the right side, you see the code from the slide again. In line 1, you define the variable
counter with the value
0. Then you define the function
update_counter() and increase the variable
1. In line 6, you call
00:55 Let’s run the file and see what happens.
You get an
UnboundLocalError, and Python says the local variable
counter was referenced before assignment. And as it seems, the issue is in line 4. But wait a minute.
According to the LEGB rule, Python should have recognized that the name
counter doesn’t exist in the
update_counter() function’s local scope and then moved up to the global scope to resolve the name. Right?
Well, apparently not. The problem here is that the code attempts to make an assignment to the variable
counter in line 4, which creates a new name in the local scope.
Then when Python executes the right-hand side of the assignment, it finds the name
counter in the local scope with nothing assigned to it yet, and that’s when Python raises the error.
If you want this code to work the way we expected at first, then we can use the
global statement. So before we assign
counter in line 5, we add the statement
If we now run the code, then it works, so there is no error right now. To actually have some output, let’s add a
print() function in line 6,
print(counter), so we see the current value of the
So when you run the file, then you see on the left side the integer
1, which means our
global counter was increased by one. To actually show you that this is the value of the global variable
counter, let’s add the
print() function to the last line of the file.
When we run the file, the integer
1 is printed again. Okay, so why does this work now and it didn’t work before? In line 4, the
global keyword tells Python to look into the global scope for the name
That way, the line 5,
counter = counter + 1, doesn’t create a new local variable. It works with the global variable. But although this fixes the program, the use of the
global keyword is considered bad form in general, because imagine that you have a big program and you wouldn’t have just one function but multiple functions and the bunch of code around it, and one tiny function changes your global
counter variable. That makes your code difficult to understand and hard to debug in the long run.
So if you find yourself using the
global keyword to fix problems like the one here, then stop and think if there is a better way to write your code.
04:00 And that’s what we will explore in the next lesson, where I will show you how to prevent pitfalls when you work with scopes in Python.
Become a Member to join the conversation.