Thursday, April 23, 2020

Local and Global and Nonlocal Variables

Let see what python doc has to say:
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global[R]

In the below example, we can see x is global variable, and is accessible inside the function.


x=10
def m1():
print(x)


Test Cases
Set x inside the function.


x=10 #This is global variable
 def m1():
     x=20 #Setting the value of x
print(x)
The value of x within the function is changed i.e 20

Accessing the value of x outside the function.
x=10 #This is global variable
 def m1():
     x=20 #Setting the value of x
print(x) #Printing the value of x in function
print(x) #Accessing the value of x outside the function.

The value of x outside the function is printed as 10 

This clearly means the variable x inside and outside the functions are not same.
This can be proved by their memory addresses.

x=10#This is global variable
def  m1():
     x=20#This is local variable
     print(x)
     print(id(x))# The Id of local variable  267516128
    
m1()
print(id(x))# the id of global variable 267515968

Output:
267516128
267515968

















The two different memory address can be seen for the same variable x.
This proves that the variable x, is not same, they are different.
The first one is global and the other one which is inside function is local.

If we want to access global variable inside the functions, then we have to explicitly declare the variable as global .


x=10#This is global variable
def  m1():
    global x
     x=20#This is again the global variable
     print(x)
     print(id(x))
    
m1()
print(id(x))
print(x)


Let’s revisit what python doc has to say:

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global[R]



Understanding nonlocal variable

First understand the below code construct.

def m1():
  x=20  #This is local variable in function m1
  print("The value of x in m1 ",x)
  print ("The value of id of x in m1",id(x))
  def  m2():
    x=30 #this is local variable in function m2
    print ("The value of x in m2 ",x)
    print ("The value of id of x in m2",id(x))
  m2()  #Calling m2 from m1
  print ("The value of x in m1 ",x)# this is after x is updated in m2
  print ("The value of id of x in m1",id(x))
m1()#calling m1 from module

Output:

The value of x in m1  20
The value of id of x in m1 1453258976
The value of x in m2  30
The value of id of x in m2 1453259136
The value of x in m1  20  #the changed value of x=30 is NOT reflected in m1
The value of id of x in m1 1453258976


  • There are two functions m1 and m2. Function m2 is subfunction of m1.
  • Function m1 has a variable x having value 20.
  • Function m2 has also same name variable x having value 30.
  • When m2 is called from m1, variable x is set to 30.
  • When trying to access the value of x from m1, the value of x is still 20.
  • This means that change in value of x in m2 in local to m2 and is not propogated to m1.
  • What if we change the value of x in m2, it should also reflect in m1.
  • For this set the variable x as nonlocal  in m2.



  def  m2():
    nonlocal x
    x=30 #this is local variable in function m2
    print ("The value of x in m2 ",x)
    print ("The value of id of x in m2",id(x))
  m2()  #Calling m2 from m1
  print ("The value of x in m1 ",x)# this is after x is updated in m2
  print ("The value of id of x in m1",id(x))
m1()#calling m1 from module

Output:

The value of x in m1  20
The value of id of x in m1 1453258976
The value of x in m2  30
The value of id of x in m2 1453259136
The value of x in m1  30             #the changed value of x=30 is reflected in m1

The value of id of x in m1 1453259136

After declaring  the variable x as nonlocal in m2, the changed value of x=30 in m2 is reflected in m1.

No comments:

Post a Comment