Python Global, Local and Nonlocal Variables

By | April 20, 2019

In this tutorial, we will discuss the variable use in python and how they are categorized in Global, local and Non-local.

Table of Content

  • Python Global Variable
  • Python Local Variable
  • global keyword in python
  • Python Non-Local variables

Python Global Variables

All those variables in python which are declared outside the function in a global scope are known asa global variable. In simple words any statement of the programme that can access a variable that variable is a global variable.

Let’s understand it with an example:

 global_variable = 20                                      #Global Variable

def func():           #defining a function
    print("the value of global_variable is " + str(global_variable))

func()                    #function calling

#Output

the value of global_variable is 20

Behind the code:

In the above example, we have first we declared a variable global_variable and assigned value 20 to it and then we defined a function func() which is printing the output value of global_variable.

So what is happening here, when we are calling the function func() the function func() gets started to execute and it reaches to the print statement and at the print() statement first the function looks for the globle_variable inside itself but when it could not find it inside the function func() then it starts looking for it outside the function and then print the value.

Local Variable

All those variables that we defined inside the function are known as a local variable and you cannot access them outside the function.

Let’s understand it with an example:

g = "global variable"

def local():
    l = "local variable"
    print(g)    # printing global variable
    print(l)    # print local variable

local()

#Output

global variable
local variable

Behind the code:

In the above example, g is a global variable and l is a local variable. You can see that we can access the g inside the function local and we can access it outside the function but if you try to access the variable l outside the function it will throw an error.

Let’s call a local variable outside the function:

def local():
    l = "local variable"                       # l is a local variable

local()
print(l)                  # Accessing local variable outside the function

 

#Output

NameError: name 'l' is not defined

Behind the code:

As we said earlier that we cannot access the local variable outside the function. if we try to access it, we will get an error such as variable not defined.

Global keyword in python

global is a keyword in python which is used to access the actual global variable from outside the function. Until now we know that we can access the global variable from outside the function but if you alter the global variable or assign a new object or value to it inside the function it will become a local variable and this has no impact on the actual global variable which is present outside the function.

if you want o to grab a variable which is outside the function and want to make changes on there we need a keyword global which can grab the global variable and we can alter them inside the function.

let’s understand it with an example.

global_variable_1 = 20
global_variable_2 = 30

def local():

    global global_variable_1    #here we have access the actual global_variable_1 which is present outside the function
    global_variable_2 = 70      #it is a local variable having the same name as the global variable
    global_variable_1 = 80      #here we have changed the value of globle_variable_1 which will reflect outside the function too


local()
print("the value of global_variable_1 has become",  global_variable_1)
print("the value of global_variable_2 did not change" ,  global_variable_2)

#Output

the value of global_variable_1 has become 80
the value of global_variable_2 did not change 30

Behind the code:

In the above example we use the global keyword to access only one global variable which is global_variable_1 and all those changes we make on global_variable_1 remain same though we change the value of variable global_variable_2  but it changes for the local() function itself it did not reflect it outside the function like global_variable_1 because we did not grab it from outside the function it is a local variable having the same name as global variable but it has nothing to do with the actual global_variable_2.  

Let’s take another example but here we do not use the global keyword:

var = 30    # global variable

def local():
    var = 30    #local variable
    var = var +40
    print("var inside the local function",var)

local()
print("var outside the function", var)

#Output:

var inside the local function 70
var outside the function 30

Same example with global keyword

var = 30    # global variable

def local():
    global var   #global variable
    var =var+ 40
    print("var inside the local function ",var)

local()
print("var outside the function", var)

#Output

var inside the function local 70
var outside the function 70

Nonlocal Variable

nonlocal is a keyword which acts as a global keyword for the nested function (function inside another function). We use the nonlocal keyword when a variable is in local scope but act as a global scope for another function.

let’s understand it with an example:

def local():
    x = 70
    
    def in_local():
        nonlocal x      #here we grab the actual x variable which is a local variable
        x = 70000
        print("value of x inside the in_local function is", x)

    in_local()      #calling function in_local
    print("the value of x inside the function local has become", x)

local()

#Output

value of x inside the in_local function is 70000
the value of x inside the function local has become 70000

Behind the function:

Though variable x of local() function is a local variable but it act as global variable for the function in_local(). When we use the keyword nonlocal to grab the variable x and use it inside the nested function in_local() and every change we make inside the in_local function on variable x reflect back on the original value of x.

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

 

Leave a Reply

Your email address will not be published. Required fields are marked *