Use of nonlocal vs use of global keyword in Python

Prerequisites: Global and Local Variables in Python

Before moving to nonlocal and global in Python. Let us consider some basic scenarios in nested functions.

filter_none

edit
close

play_arrow

link
brightness_4
code

def fun():
    var1 = 10
  
    def gun():
        print(var1)
        var2 = var1 + 1
        print(var2)
  
    gun()
fun()

chevron_right


Output:

10
11

The variable var1 has scope across entire fun(). It will be accessible from nested function of fun()

filter_none

edit
close

play_arrow

link
brightness_4
code

def fun():
    var1 = 10
  
    def gun():
        # gun() initializes a new variable var1.
        var1 = 20
        print(var1, id(var1))
  
    print(var1, id(var1))
    gun()
fun()

chevron_right


Output:



10 10853920
20 10854240

In this case gun() initialized new variable var1 in the scope of gun. var1 with value 10 and var1 with value 20 are two different and unique variables. var1 holding value 20 will be by default accessed in gun().

Considering the previous example, we know that guns will initialize a new variable var1 in its own scope. But when it is going to do so, it cannot find the value of var1 yet,
to perform arithmetic operation as no value has been assigned to var1 previously in gun().

filter_none

edit
close

play_arrow

link
brightness_4
code

def fun():
    var1 = 10
  
    def gun():
        # tell python explicitly that it 
        # has to access var1 initialized 
        # in fun on line 2
        # using the keyword nonlocal
        nonlocal var1
        var1 = var1 + 10
        print(var1)
  
    gun()
fun()

chevron_right


Output:

20

In this example, before initializing var1 in gun(). We have explicitly told Python that, do not initialize a new variable, instead access var1 present already on line 2. using the keyword nonlocal So when interpreter performs addition, it access the value 10(already present) and error is avoided.

Let us move on to global keyword now. Consider the examples given below

filter_none

edit
close

play_arrow

link
brightness_4
code

var1 = 10
def fun():
    # global variable var1 will 
# be read or accessed in fun()
    print('var1 is', var1)
  
fun()

chevron_right


Output:

var1 is 10

Global variable var1 will be simply read or accessed inside function fun()

filter_none

edit
close

play_arrow

link
brightness_4
code

var1 = 10
def fun():
    # new local variable var1 
    # will be initialized in fun()
    var1 = 20
    print('var1 is', var1)
    print('var1 is at', id(var1))
  
fun()
print('var1 is', var1)
print('var1 is at', id(var1))

chevron_right


Output:

var1 is 20
var1 is at 10854240
var1 is 10
var1 is at 10853920

A new variable var1 will be initialized in fun(). Global variable var1 will be different from local variable var1 of fun(). By default in fun(), the local variables will be accessed.


Considering previous example, we know that fun() will initialize a new variable var1 in its own scope. But when it is going to do so, it cannot find the value of var1 yet,
to perform arithmetic operation as no value has been assigned to var1 previously in fun()

filter_none

edit
close

play_arrow

link
brightness_4
code

var1 = 10
def fun():
    # tell python explicitly do not 
    # intialise a new variable
    # instead access var1 which 
    # has global scope
    global var1
    var1 = var1 + 20
    print('var1 is', var1)
  
fun()

chevron_right


Output:

var1 is 30

In this example, before initializing var1 in fun(). We have explicitly told python that, do not initialize a new variable, instead access var1 present already on line 1. using the keyword global So when interpreter performs addition, it access the value 10(already present) and error is avoided.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.