Lesson Contents
Python has two scopes for variables:
- Local scope: variables you create within a function are only available within the function.
- Global scope: variables you create outside of a function belong to the global scope and can be used everywhere.
In this lesson, I’ll show you the difference between the two scopes with examples.
Local Scope
Let’s start with the local scope. Take a look at the following code:
def test_local_scope():
test_string = "local scope"
print("executed from function: " + test_string)
test_local_scope()
print("executed globally: " + test_string)
Will show this output:
Traceback (most recent call last):
File "main.py", line 7, in
print("executed globally: " + test_string)
NameError: name 'test_string' is not defined
Above, we see the print message that ran from within the function. The second print fails because the variable “test_string” is local to the scope. It’s not available globally.
Global Scope
The variables you create globally can be used within a function. Here is the code:
test_string = "Global scope."
def test_global_scope():
print("Executed from function: " + test_string)
test_global_scope()
Will show this output:
Executed from function: Global scope.
Our function is able to print the contents of the global variable. There is a catch, however. The function can only read the global variable, it can’t modify it. There is an exception to this rule, which I’ll show you in a minute.
Overlapping Variable Names
What happens when you use the same variable name globally and within a function? Let’s find out:
test_string = "Global scope."
def test_global_scope():
print("Executed from function: " + test_string)
test_string = "Local scope."
test_global_scope()
Will show this output:
Traceback (most recent call last):
File "main.py", line 8, in
test_global_scope()
File "main.py", line 4, in test_global_scope
print("Executed from function: " + test_string)
UnboundLocalError: local variable 'test_string' referenced before assignment
The code above assigns the variable “test_string” globally and within a function. We receive an error message right away. Once you assign a variable within a function, Python sees it as a variable with local scope. We receive the error message because we try to print a variable that isn’t assigned yet within the function.
Let’s change our code a bit. Let’s assign the variable within the function before we try to print it:
test_string = "Global scope."
def test_global_scope():
test_string = "Local scope."
print("Executed from function: " + test_string)
test_global_scope()
print("Executed globally: " + test_string)
Will show this output:
Executed from function: Local scope.
Executed globally: Global scope.
This works. The variable has a different value, one for the global scope, and another for the local scope.
Global Variable
In the previous example, we noticed that you can read a global variable from within the function. You can’t create or modify a global variable though from within the function. There is an exception to this rule. It’s possible when you use the global keyword.
Here is our code: