Python Global, Local and Nonlocal Variables

Global Variables 

In python, the variable declared outside the function or in the global scope is a global variable. By default variables declared outside the function are global. They can be accessed anywhere in the program.

Code:

Python Code

x = 3


class tuf:
    def __init__(self) -> None:
        print("x inside the class", x)


def func():
    print("x inside the function ", x)


print("x in global scope", x)
tuf()
func()

Output:

x in global scope 3
x inside the class 3
x inside the function  3 

In the above code, ‘ x ‘ is the global variable. We can able to access the x inside the function, inside the class, and outside also. Hence the global variable is accessible anywhere in the program. 

Let’s try to change the value of a global variable inside the function 

Code : 

Python Code

x = 3

def func():
    x = x+2
    print(x)

func()

Output : 

UnboundLocalError: local variable ‘x’ referenced before assignment 

This raises errors because python doesn’t allow to change of the global variable inside the function. In order to change the value of a global variable inside a function, we should use the global keyword. 

The process of changing the global variable value inside the function is discussed in the article Python Global Keyword. If interested go through it. 

Local Variables : 

A variable declared in the local scope or inside the function is a local variable. 

The scope of the variable is restricted only to the function. It can be accessed only inside the function not anywhere else. Let’s see an example

Code : 

Python Code

def fun():
    x = 3
    print('x inside function is ', x)

fun()
print('x outside fucntion is ', x)

Output : 

x inside function is  3
NameError: name ‘x’ is not defined

The interpreter can able to identify the ‘ x ’ inside the function and cannot identify it outside the function, because ‘ x ‘ is a local variable, its scope is limited only to the function.  

Global variable and Local variable having  Same Name :

Suppose there is a global variable and local variable with the same name, let’s see what value is taken inside and outside the function. 

Code:

Python Code

def func():
    x = 3
    print('x inside function', x)


x = 5
print('x outside function ', x)
func()

Output : 

x outside function  5
x inside function 3

Inside the function, the value of the local variable ‘ x ’  is printed, and outside the function, the global variable ‘ x ’ is printed. The scope of the local variable ‘ x ’ is limited to only the function, So outside the function, the global variable value is printed. Inside the function both local and global variables are accessible.  

When a variable is used inside the function,  first python checks whether it is declared, inside the function (local scope) or not, if declared then uses local value, else checks whether the variable is global or not. If the variable is global, then uses the global value, if it is neither local nor global variable it raises an error.  

Hence Inside the function, the local value is printed, and outside the function, the global value is printed.  

Non Local Variables : 

For Non Local Variables, the local scope is not defined. They are used in nested functions. That means the nonlocal variable is neither in local scope nor global scope.

We use the nonlocal keyword to create nonlocal variables. Let’s see an  example 

Code:

Python Code

def fun1():
    x = 3

    def fun2():
        nonlocal x
        x = 4
        print("x inside fun2() is ", x)

    fun2()
    print("x inside fun1() is ", x)


fun1()

Output :

x inside fun2() is  4
x inside fun1() is  4

In the above code, ‘ x ‘ is defined as a nonlocal variable inside the fun2() and we changed the value of ‘ x ’ from 3 to 4 inside the fun2(). So Inside the fun2(), the value of ‘ x ’ is 4.

 Inside the function fun1() the value of ‘ x ’ is also 4, not 3 because fun2() is in the scope of fun1(), so ‘ x ‘ is also in the scope of fun1(). In the scope of fun1() the value of ‘ x ‘ is changed, so the value of ‘ x ’ got changed in the fun1() also. 

Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]