Python Global, Local and Nonlocal Variables
Global Variables
In Python, a variable defined outside the function or in global scope is identified as a global variable.
A global variable can be accessed inside or outside the function. But you can not change the value of a global variable inside a function.
Example: Create a Global Variable
In the following example, we will create a global variable.
x = "global"
def my_func():
print("x inside:", x)
my_func()
print("x outside:", x)
Output
x inside: global
x outside: global
In the above example, we created x
as a global variable and defined a my_func
to print the global variable x
. And we call the my_func
, which will print the value of x
.
Let us see what will happen if we change the value of x
inside a function.
x = "global"
def my_func():
x = x * 3
print("x inside:", x)
my_func()
Output
UnboundLocalError: local variable 'x' referenced before assignment
As we can see above, the code raises an error because Python treats x
inside my_func()
as a local variable, and x
is not defined inside my_func()
.
To change the global variable x
inside the my_func()
, we must use the global
keyword. To learn more visit Python Global Keyword.
Note: To change the value of a global variable inside a function, you must use the
global
keyword.
Local Variables
A variable declared inside the function's body or in the local scope is identified as a local variable.
Example: Accessing local variable outside the scope
In the following example, we will try to access a local variable from outside the scope.
def my_func():
y = "local"
my_func()
print(y)
Output
NameError: name 'y' is not defined
As we can see above, an error is raised because we are trying to access a local variable y
in a global scope while the local variable only works inside my_func
or local scope.
Example: Create a Local Variable
In the following example, we will declare a variable inside the function to create a local variable.
def my_func():
y = "local"
print(y)
my_func()
Output
local
Global and Local Variables
Python allows mixing the global and local variables in the same code.
Example: Using Global and Local Variables in the same code
In the following example, we will use the global and local variables in the same code.
x = "global "
def my_func():
global x
y = "local"
x = x * 3
print(x)
print(y)
my_func()
Output
global global global
local
As we can see above, we declare x
as a global and y
as a local variable inside the my_func()
. And we used the multiplication operator *
to modify the global variable x
and we print both x
and y
.
After calling the my_func()
, the value of x
becomes global global global
because we used the x * 3
to print three times global
. And we print the value of the local variable y
, which is local
.
Example: Using Global and Local Variable with the same name
In the following example, we will use a global and local variable with the same name.
x = 9
def my_func():
x = 14
print("local x: ", x)
my_func()
print("global x: ", x)
Output
local x: 14
global x: 9
As we can see above, we used the same name x
for both the global variable and the local variable. We notice different results when we print the same variable because the variable is declared in both scopes, the global scope outside my_func()
and the local scope inside my_func()
.
When we print the variable x
inside my_func()
it outputs local x: 14
. This is the local scope of the variable.
In the same way, when we print the variable x
outside my_func()
, it outputs global x: 9
. This is the global scope of the variable.
Nonlocal Variables
In Python, nonlocal variables are used in nested functions where the local scope is not defined.
The nonlocal variable can be neither in the local neither the global scope.
To create a nonlocal variable, we used the nonlocal
keyword.
Let us see an example of how we used a nonlocal variable.
Example: Create a nonlocal variable
In the following example, we will create a nonlocal variable.
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
Output
inner: nonlocal
outer: nonlocal
In the above code, the inner()
function is a nested function. We used the nonlocal
keyword to create a nonlocal variable. The inner()
function is defined in the scope of another function outer()
.
Note: If we update the value of a nonlocal variable, the changes also appear in the local variable.