Page Contents
In this tutorial we will learn about global and nonlocal keyword in Python . Before going through this tutorial we need to know about namespace and scope in Python .
Global keyword in Python
Global keyword in Python helps us to change value of a global variable inside a function . First we will write a program to change the value of a global variable inside a function without global
keyword and check whether the program actually change the value of the global variable or not .
# declaring a global variable
x=20
# defining a function
def change():
# try to change the value of x
x=15
# call the function
change()
# after function calling value
# of x should change
print(x)
20
- In the above code first we have declared a global variable named
x
as value20
. - Then we have written a function named
change
to change the value ofx
. After calling the function the value ofx
should be changed to15
. - After that we have called the function and printed the value of
x
. From the output we can see that the value ofx
is not changed .
Now we will try to understand what happens here –
- When we try to change the value of global variable
x
by writingx=15
, instead of changing the value of global variable a new local variable namedx
is created with value15
. So now the program have twox
variable one is global variable with value20
and other is local variable with value15
. - For this reason when we have printed
x
variable outside the function it has printed global variablex
whose value is20
.
Now we will use global keyword and change the value of a global variable inside a function –
# declaring a global variable
x=20
# defining a function
def change():
# change the value of x
global x
x=15
# call the function
change()
# after function calling value
# of x should change
print(x)
15
- In the above code first we have declared a global variable named
x
as value20
. - Then we have written a function named
change
to change the value ofx
. As we have usedglobal
keyword beforex
so Python has treated this x as global variable instead of local variable . Then we have changed the value ofx
to15
. - After that we have called the function and printed the value of
x
. From the output we can see that the value ofx
is changed .
Use of global keyword in nested function
# declaring a nested function
def outer_func():
x=20
def inner_func():
global x
x=15
return inner_func()
# calling the function
outer_func()
# printing the x
print(x)
15
- In the above code first we have defined a function named
outer_func
which returnedinner_func()
. When we have writtenx=20
a local variablex
is created with value20
. - Then we have defined a function named
inner_func
inside this function . In theinner_func
when we have writtenglobal x
, a global variablex
is created . After that we have assigned15
with the global variablex
. - After that we have called the
outer_func()
function and printed the value ofx
. As we have printedx
outside the function so the global variablex
will be printed whose value is15
. From the output we can see that15
is printed in the output .
Nonlocal keyword in Python
In a nested function , if we define a variable inside an inner function then this variable is not accessible from the outer function . But to access the variable defined in the inner function from the outer function we use nonlocal
keyword . Nonlocal means it is not local . It means that any variable defined any depth of inner function is accessible from the outer function . But it also means that it is not global so we can not access nonlocal variable from the outside of the outer function .
At first we will try to access any variable defined in the inner function from the outer function without using nonlocal keyword .
# declaring a nested function
def outer_func():
x=20
y=10
def inner_func():
y=15
inner_func()
return x+y
# calling the function
print(outer_func())
30
- In the above code first we have defined a function named
outer_func
. - When we have written
x=20
, a local variablex
with value20
has been created . - After that we have written
y=10
, so a local variabley
with value10
has been created . - Then we have defined a function named
inner_func
and called the function . During calling a new local variabley
with value15
is created which can be accessible only frominner_func
instead of changing they
variable ofouter_func
. After callingy
variable will not exist . - After that we have added
x
andy
which printed 30 in the output .
Examples of nonlocal keyword in Python
Example 1:
# declaring a nested function
def outer_func():
x=20
y=10
def inner_func():
nonlocal y
y=15
inner_func()
return x+y
# calling the function
print(outer_func())
35
This code is same as the previous code only change is we have used nonlocal
keyword . As we have used nonlocal
keyword so value of local variable y
is changed to 15
after inner_func
calling . For this reason it is printed 35
in the output .
Example 2: Chaining of nonlocal keyword in nested function
# declaring a nested function
def outer_func():
x=20
def inner_func():
nonlocal x
x=10
def inner_inner_func():
nonlocal x
x=15
inner_inner_func()
inner_func()
return x
# calling the function
print(outer_func())
15
Example 3:
We must declare a variable in the outer function before use nonlocal
keyword with this variable in inner function otherwise it will raise SyntaxError
.
# declaring a nested function
def outer_func():
x=10
def inner_func():
nonlocal y
y=15
inner_func()
return x+y
# calling the function
print(outer_func())
File "C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_7872/3746586655.py", line 5 nonlocal y ^ SyntaxError: no binding for nonlocal 'y' found
In the above code we have used nonlocal y
in the inner_func
but not declared y
variable in the outer_func
. So the program raised SyntaxError
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .