Saturday, December 28, 2024
Google search engine
HomeLanguagesUse of nonlocal vs use of global keyword in Python

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.

Python3




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


Output: 

10
11

 

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

Python3




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


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 the arithmetic operation as no value has been assigned to var1 previously in gun(). 

Python3




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()


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 the 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 

Python3




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


Output: 

var1 is 10

 

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

Python3




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))


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()

Python3




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


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.
 

RELATED ARTICLES

Most Popular

Recent Comments