Python 3x Pandas Django

Python Scope


The part of a program where a variable is accessible is called its scope

Global and local variables

a. Global variables are the ones that are defined and declared outside any function and are not specified to any function. They can be used by any part of the program

b. Local variable created inside a function belongs to the local scope of that function, and can only be used inside that function

Global Scope Example

x = 300

def myfunc1():
  print(x)

def myfunc2():
  print(x)

myfunc1()
myfunc1()

print(x)

Output:

#Output:
300
300
300

In the above example, variable x is accessing across all the function and main program as it declared globally

Local Scope Example

def myfunc1():
  x = 300
  print(x)

def myfunc2():
  x = 400
  print(x)

myfunc1()
myfunc2()

print(x)

Output:

#Output:
300
400
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in 
--->  print(x)

NameError: name 'x' is not defined


In the above example, x in myfunc1() is accessible myfunc1() only and, x in myfunc2() is accessible myfunc2() only so, print(x) in the main program is got a NameError as there is no x variable defined in the main block

Global & Local Variables

x = 30

def myfunc1():
  x = 300
  return x

print(x)            #Output: 30
print(myfunc1())    #Output: 300

In the above example, print(x) statement prints 30 because local scope of x in myfunc1 scope available to that function alone. so it picks the value from global variable x.

Scope should be accessible in the order of local scope, parent, global & python build-in functions.

Example1
x = 30

def myfunc():
  x = 300
  def insidefunc():
    return x
  return insidefunc()

print(x)            #Output: 30
print(myfunc())     #Output: 300

In the above example, print(x) statement prints global scope variable x as there is no local and parent scopes avaiable to that place and print(myfunc()) statement prints parent scope variable as there is no local scope.

Example2
def myfunc():
  def insidefunc():
    return sum
  return insidefunc()

print(myfunc())     #Output: < built-in function sum >

In the above example, print(myfunc()) statement prints python built-in function as there is no local, parent & global variable sum.

What is the global keyword

Global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.

a. When we create a variable inside a function, it is local by default.

b. When we define a variable outside of a function, it is global by default. You don't have to use global keyword.

c. We use global keyword to read and write a global variable inside a function.

d. Use of global keyword outside a function has no effect.


Let's take an example of where the global keyword requires

Accessing global variable from inside a function

a = 1  #Global Variable

def myfunc():
    print(a)

myfunc()   #Output: 1

The output is 1. However, we may have some scenarios where we need to modify the global variable from inside a function.

Modifying global variable from inside the function

a = 1 # global variable

def mul():
    a = a * 10 # multiply c by 10
    print(a)

mul()

Output:

    def mul():
---->      a = a * 10 # multiply c by 10
           print(a)

UnboundLocalError: local variable 'a' referenced before assignment

This is because we can only access the global variable but cannot modify it from inside the function

Here is the solution for this is to use the global keyword.

Changing global variable from inside a function using global

a = 10 #global variable

def mul():
    global a
    a = a * 10 # multiply c by 10
    print("Inside Func.", a)

mul()
print("MainBlock", a)

Output:

Inside Func. 100
MainBlock 100

In the above program, we define variable a as a global keyword inside the mul() function

Then, we multiply the variable a by 10, i.e c = c * 10. After that, we call the mul() function

Finally, we print the global variable 'a' in the main block and inside mul function

As we define the global keyword, You can see the change also occurred on the global variable outside the function, c = 100

If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!