Skip to content

Global and Nonlocal keyword in Python

Global and nonlocal keyword in Python

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 value 20 .
  • Then we have written a function named change to change the value of x . After calling the function the value of x should be changed to 15 .
  • After that we have called the function and printed the value of x . From the output we can see that the value of x is not changed .

Now we will try to understand what happens here –

  • When we try to change the value of global variable x by writing x=15 , instead of changing the value of global variable a new local variable named x is created with value 15 . So now the program have two x variable one is global variable with value 20 and other is local variable with value 15 .
  • For this reason when we have printed x variable outside the function it has printed global variable x whose value is 20 .

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 value 20 .
  • Then we have written a function named change to change the value of x . As we have used global keyword before x so Python has treated this x as global variable instead of local variable . Then we have changed the value of x to 15 .
  • After that we have called the function and printed the value of x . From the output we can see that the value of x 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 returned inner_func() . When we have written x=20 a local variable x is created with value 20 .
  • Then we have defined a function named inner_func inside this function . In the inner_func when we have written global x , a global variable x is created . After that we have assigned 15 with the global variable x .
  • After that we have called the outer_func() function and printed the value of x . As we have printed x outside the function so the global variable x will be printed whose value is 15 . From the output we can see that 15 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 variable x with value 20 has been created .
  • After that we have written y=10 , so a local variable y with value 10 has been created .
  • Then we have defined a function named inner_func and called the function . During calling a new local variable y with value 15 is created which can be accessible only from inner_func instead of changing the y variable of outer_func . After calling y variable will not exist .
  • After that we have added x and y 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 .

Leave a Reply