CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture


Variable scope in Python

Scope Definition

Part of the program within which a name is legal and accessible is called the scope of name. All variables in Python are objects. So in this post, we are considering Variables in place of Objects. 

To understand the scope of variables, let us discuss the following example:

# CASE - 1
var = 9
def fun():
    print("inside :", var)  ## inside : 9
print("Outside :" ,var)     ## Outside : 9

# CASE - 2
var = 9
def fun():
    var = 10
    print("inside :", var)  ## inside : 10
print("Outside :" ,var)     ## Outside : 9

In the above example, there are two cases. In CASE-1, var is declared outside the function fun(). In CASE-2, two var are declared, one inside the fun() and second outside the fun()

If you compare the output in both CASES, you will find that in CASE-2, the value of var is taken from inside the function. But, in the case of CASE-1, the value of var was taken from outside the function, because there was no Local variable var was available.

So, this is called the scope of variables. First, the variable will be searched from within the function. Then if Local Variable is not available then python will search the variable outside the function.

In the CASE-2, the value of var was changed to 10 inside the function. But, it did not affect the value of var outside the function. Because a new copy of var was created inside the function. When the python control came out of the function the variable var (which was created inside the function) got destroyed. That is why the value of var outside the function displayed 9.     

Types of Variables with respect to its Scope

There are three types of variables in respect of Scope:
  1. Local Variable
  2. Global Variable
  3. Non-Local Variable
Let us now discuss each type of variable one by one.

  1. Local Variable: Local variable is a variable, which is defined inside a block (ex:- a Function) and cannot be accessible outside that block.

    In the following example, Y is a local variable to function foo().
    # variable y is a local variable
    def foo():
        y = "local 19"
        print(y)       ##Output: local 9

  2. Global Variable: In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed from inside and outside of the function.
    # Here X is a Global Variable
    X = "Global"
    def foo():
        print("X Inside:", X)   ## X Inside: Global
    print("X Outside:", X)      ## X Outside: Global

    In the above example, 'X' is a Global Variable. It is accessible within the foo() and outside the foo(). That means 'X' is accessible from throughout the program.

    Using Local and Global Variables Together:

    # Using global and local variable togather with same name
    def foo():
        X = 10
        print("Local X:", X)  ## Local X: 10
    print("Global X:", X)  ## NameError: name 'X' is not defined

    From the above example, you can understand if we define a variable inside a function and try to access it from outside the function, we will get the error 'X' is not defined. Because once the control comes out of the function foo() all the variables declared inside the function get destroyed.

    Global Keyword:

    When we declare the variable outside the function and try to change it from within the function, it will not change the value. To change the Global value from inside a function we will use the global keyword.

    ## Without 'global keyword'
    X = "Global"
    def foo():
        X = 'Local'
        print("X Inside:", X)   ## X Inside: Local
    print("X Outside:", X)      ## X Outside: Global
    ## With 'global keyword'
    X = "Global"
    def foo():
        global X
        X = 'Local'
        print("X Inside:", X)   ## X Inside: Local
    print("X Outside:", X)      ## X Outside: Local

  3. Non-Local Variable: Nonlocal Variable is used in a nested function whose local scope is not defined. This means that the variable can be neither in the local nor the global scope.

    We can understand the working of a non-local variable by the following example.

    # variable x is working as Global,Local and NonLocal
    1. x = "Global"
    2. def outer():
    3.    x = "local"
    4.    def inner():
    5.        nonlocal x
    6.        x = "nonlocal"
    7.        print("inner:", x) ## inner: nonlocal
    8.    inner()
    9.    print("outer:", x)   ## outer: nonlocal
    11. outer()
    12. print(x)          ## Global

    In the above example, at line 1 we have declared a variable x with values as "Global". Then there are two functions outer() and inner(). In both the functions, we have declared two variables with values "local" (line 3) and "nonlocal"(line 6). All the variables are having same name 'x'.

    which is declared at line 5, I am using a keyword nonlocal x this is telling the python to use the variable x which is just one scope above i.e. variable x of the outer() function. So, instead of creating its own copy of 'x' outer inner() will use the variable of the outer() function.

    That is why at line 7 and line 9, both are having the value of 'x' as nonlocal. At line 12, value is coming from Global Variable 'x' which is declared at line 1.

No comments:

Post a Comment