Scope of a variable, global and non-local keyword in python

In this tutorial, you are going to learn about the scope of a variable, global and non-local keyword in Python.

Scope of a variable in Python

All variables in program may not be accessible at all the location of the program. There are specific portion where you can access these variables without any error. There are two basic scopes of a variable in python:

  1. Global variable
  2. Local variable

Global variable:

Global variable are those variables which are declared in the main program and are accessible everywhere, i.e., inside and outside the function of the program.

The given program will explain you the concept of global variable more clearly. The program has a variable ‘b’ which is a global variable and it is accessible inside the as the function is printing the value of ‘b’ and outside too as we have printed the value of b after the function call.

# Global variable
b=20

# Function definition
def msg():

    # Since the variable is global it will be accessible here
    print("value of b is:",b)
    return

# Function call
msg()
print(b)  

Output:-

value of b is: 20
20

 

Local variable:

The local variables are those variables which are declared in a loop or function and are only accessible there. If you try to access these variables outside the loop or function, then it will create an error.

The program explains that the variable ‘a’ is a local variable because it is declared inside the function. The variable ‘a’ will be accessible only inside the function. So, when we try to print the value of ‘a’ outside the function this will show an error. The error will ‘a’ not defined which you can see in the output too.

# Function definition
def msg():
    
    # local variable
    a=10
    print("value of a is:",a)
    return

# Function calling
msg()
print(a)  # it will show an error because a is local variable

Output:-

value of a is: 10
Traceback (most recent call last):
File "C:\Users\user\Desktop\rev.py", line 6, in <module>
print(a) # it will show an error since variable is local
NameError: name 'a' is not defined

 

Global keyword:

Global keyword is used to declare that a variable inside the function or loop is global.

Program without the global keyword –

a=10 # Global variable

# Function definition
def fun_1():

    # local variable
    a=5
    print(a)
    return

# Function call
fun_1()
print(a)

Output:-

5
10

Program with the global keyword
Since we declare the variable ‘a’ as a global inside the function. So, a=5 becomes a global variable and we will get the value 5 when we print it inside the function or outside the function.

a=10

# Function definition
def fun_1():

    # defining the variable 'a' as the global variable inside the function
    global a 
    a=5
    print(a)
    return

# Function call
fun_1()
print(a)

Output:-

5
5

 

Non-local keyword:

The use of non-local keyword is very much similar to the global keyword. Non-local keyword will declare a variable inside a nested function( function inside a function) is not local to it, meaning it lies in the outer enclosing of the function. If we need to notify the value of a non local variable inside a nested function, then we must declare it with non-local otherwise a local variable with that name is created inside the nested function.

Program without using non-local keyword –

# Function definition 1
def outer_function():

    # local variable but global variable for the inner function()
    a=10

    # Function definition 2
    def inner_function():

        # local variable
        a=5
        print("Inner function:",a)

    # Calling of function 2
    inner_function()
    print("outer function:",a)

# calling of function 1
outer_function()

Output:-

5
10

Program using non-local keyword –
The variable ‘a’ has defined as the non-local variable inside the inner function(). So, when we try to print the value inside or outside the function, then it will only print the value 5 which is declared inside the inner function().

# Function definition 1
def outer_function():
   
    # Local variable
    a=10

    # Function definition 2
    def inner_function():

        # defining the variable 'a' as the non-local variable
        nonlocal a
        a=5
        print("Inner function:",a)
    inner_function()
    print("outer function:",a)
outer_function()

Output:-

Inner function: 5
outer function: 5

Go and check other tutorials on python:

https://www.codespeedy.com

Leave a Reply

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