Global and Local Variables in Python

Learn With Examples what are the difference between Local Variable Vs Global Variable
Written by Paayi Tech |10-Apr-2019 | 0 Comments | 591 Views

Before diving into the classes we must know about what is local and what is global variables. How they are accessed in python programming language and what are the limitation of these variable.

 

Local Variable:

Local variable is a variable that we normally assign. Its value is only valid to that scope and can’t be accessed outside the scope. Following example will clear the concept of local variables.

 

out = "outside"
def local():
    ins = "inside"
    print(f"from function {out}")
    print(f"from function {ins}")
   
local()
print(f"from sequential {out}")
print(f"from sequential {ins}")

 

We assign the variable out outside the function and before defining it and in inside the function scope. Function can access both the variable but when we try to print the ‘in’ variable outside the scope it generates an error. The output of the above written program is following:

 

#Output

 

from function outside

from function inside

from sequential outside

 

Traceback (most recent call last):

  File "/home/lalatoofani/program.py", line 9, in

    print(f"from sequential {inside}")

NameError: name 'inside' is not defined

 

As we can see that both print function inside the function body is executed and ‘out’ variable is executed in sequential print but ‘in’ variable is not printed. This is because python do not access the function variable.

 

Global Variable:

Global variable is a variable that can be accessed from every. There is no boundary of scope that affect the global variable. The above example can be converted into such example so that we can access all the variables even outside the block.

out = "outside"
def local():
    ins = "inside"
    print(f"from function {out}")
    print(f"from function {ins}")
   
local()
print(f"from sequential {out}")
print(f"from sequential {ins}")

 

#Output

 

from function outside

from function inside

from sequential outside

from sequential inside

 

 

NonLocal Variable:

Non-local variables are those variables which make changes the module level. The type of changes is permanent at module level. It cannot be declared outside the module. Unlike global variable which can be defined anywhere nonlocal can only be defined within a function block. Following example will show collectively how different are local, nonlocal and global variable from each other.

 

def scope_func():
    def local_func():
        variable = "local variable"
 
    def nonlocal_func():
        nonlocal variable
        variable = "nonlocal variable"
 
    def global_func():
        global variable
        variable = "global variable"
 
    variable = "test variable"
    local_func()
    print(f"After local assignment: {variable}")
    nonlocal_func()
    print(f"After nonlocal assignment: {variable}")
    global_func()
    print(f"After global assignment: {variable}")
 
scope_func()
print(f"In global scope: {variable}")
 

 

#Output:

After local assignment: test variable

After nonlocal assignment: nonlocal variable

After global assignment: nonlocal variable

In global scope: global variable

 

As we can see in the output section the nonlocal change function binding and even after global changing there is no changing within a function. Global is than accessed outside the block. In this way we can control the data integrity. We can control who can see the data and who cannot.

 





Login/Sign Up

Comments




Related Posts



© Copyright 2019, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.