Introduction to Global and Local Scope
In this lesson, you'll learn about about the concept of global and local scopes, how to access and modify global variables inside a function, and how to use the global
keyword in Python.
We have already learnt that variables store values for later use. To access the stored value, we just have to call a variable somewhere below in our code. For example:
We can even access that variable from within a function. Let's try it:
However, the rules change slightly when we want to modify the variable inside the function.
Let's see what happens if we try to:
Even after calling the function, the variable text still holds its original value.
It seems like nothing changed, even though we assigned a new value to text inside the function.
To understand what happened here, you need to know that we are dealing with two so-called scopes.
A scope describes a region of code from which you can access certain variables or functions.
When we first created the variable text, we were creating it in the global scope.
The global scope is the outermost scope in your program. Whenever you create a variable outside a function (or a class), you are defining a global variable within the global scope.
However, when you create a function, the code inside the function has its own scope, known as local scope.
You can access all global variables inside the local scope of a function. For example:
However, when we create a variable inside the function, we're defining a so-called local variable that can only be accessed in the local scope of that function.
For example, here we create a local variable my_num
inside the local scope of my_function
. When we try to access that variable outside the function, we run into an error:
Back to our original example and to why the value of text does not change:
The reason is that we have actually created 2 versions of text: one in the global scope and another that exists only within the scope of my_function
.
Here, you can see that the string Python
is only assigned to the local version of text
, while the global version remains unchanged before and after we call my_function
:
To tell Python that you want to modify a global variable inside a function instead of creating a new one with the same name, you can use the global
keyword.
You can use the global
keyword to declare a variable as global within a local scope:
This allows my_var
to be accessed and modified globally.
So, to modify the global variable text
from our example, we need to declare it as global inside my_function
:
Now, we have successfully modified text
from within the scope of my_function
.
Using the global
keyword, you can not only modify global variables from within local scopes, but also create new ones.
For example, in the code below, we create a new global variable inside my_function
and access it outside the function's scope:
Be aware that the variable is only created when you call the function.
It will not work if you try to access num
before calling my_function
:
What will be the output?
What will be the output?
What will be the output?
What will be the output?
What will be the output?
What will be the output?