Python Tutorials: Global Vs Local Variables in Python

Python Tutorials: What are the difference between Local Variable Vs Global Variable
Written by Paayi Tech |16-Sep-2020 | 0 Comments | 761 Views

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

 

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 inside the function scope. The 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 the 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 the ‘out’ variable is executed in sequential print, but ‘in’ variable is not printed. This is because python does not access the function variable.

 

Global Variable:

The 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 an 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:

Nonlocal variables are those variables that make changes to the module level. The type of change is permanent at the module level. It cannot be declared outside the module. Unlike a global variable, which can be defined anywhere nonlocal can only be defined within a function block. The following example will collectively show how different are local, nonlocal, and global variables 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 change, there is no change within a function. Global is then 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 2020, 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.