Functions3.3.2 Variable Scope
Functions3.3.2 Variable Scope
(Computer Science)
Chapter 3:
Working with Functions
(3. User Defined Functions)
Scope of Variables
Prepared by:
S K Kothiyal
PGT Comp Science
KV No HBK DDun
Scope of Variables
Y =20 # Y is Global
Z = Y + func(X) # Z is Global
Print(Z)
LOCAL Scope
Therefore, the life time of variable a & b is their functions run, while the life time
of variable Y & Z is the entire program.
How global & local environment created in a function call
1. def calsum(a,b,c):
2. s = a+b+c # statement 1
3. return s # statement 2
7. N1 = 10 # statement 1
8. N2 = 20 # Statement 2
9. N3 = 30 # statement 3
10. Res= avg( N1,N2,N3) # statement 4
11. print(Res) # statement 5
How global & local environment created in a function call
• Line1: def encountered ; line 2 & 3 are ignored
1. def calsum(a,b,c): • Line4: def encountered ; line 5 & 6 are ignored
2. s = a+b+c # statement • Line 7,8,9: Execution of main program begins; and global
1 environment created: N1,N2 & N3 added to it.
3. return s # statement 2 • Line 10: function avg() is invoked , so local environment for
avg() is created. Formal argument x,y,z are created in local
environment of avg()
4. def avg(x, y, z):
• Line 5: the function calcsum() is invoked and local
5. sm = calsum(x,y,z) # Statement 3 environment for calcsum is created, so formal argument
6. return sm/3 # statement a,b,c are created in local environment of calcsum()
4 • Line 1: variable s is created in local environment of
calcsum(), the sum of x, y & z is computed & stored in s
7. N1 = 10 # statement 1 • Line 2,3: the value of s is returned to sm of avg() and
8. N2 = 20 # Statement 2 calcsum() is over , hence the local environment of
9. N3 = 30 # statement 3 calcsum() is removed
10. Res= avg( N1,N2,N3) # statement 4 • Line 6: returned value is calculated as sm/3 i.e. 60/3 =20
11. print(Res) # and returned to caller ; avg() is over so its local
statement 5 environment is removed.
• Line10,11: the returned value is stored in Res and printed
in the output and prog is over. With this the global
environment of the prog is removed.
Name Resolution (Resolving scope of a name)
• Whenever we access any variable from a program or function, Python
follows name resolution rule, also known as LEGB (Local Enclosing Global
Built-in) rule. Python does the following to resolve it:
1. it checks within its local environment, if it has a variable with the same
name, Python uses its value. If not then it moves to step 2.
2. Python now checks the enclosing environment , if it has a variable with
the same name, Python uses its value, if not found in current
environment, then Python repeats this step to higher level enclosing
environment. If not then it goes to step 3.
3. Now Python checks for Global environment if it has a variable with the
same name, Python uses its value. If not then it goes to step 4.
4. Python checks its built-in environment that contains all built-in variables
and functions of Python, if it has a variable with the same name, Python
uses its value. If not then it would report an error:
name <variable> not found.
e.g. name Address not found
Case 1
• Variable in global scope but not in local scope.
def avg(x, y, z):
s = (x+y+z)/3 # Statement 3
print(N1) # variable N1 is a global variable, not a local variable , so it will print
# Global N1
return s # statement 4
N1 = 10 # statement 1
N2 = 20 # Statement 2
N3 = 30 # statement 3
Res= avg( N1,N2,N3) # statement 4
print(Res) # statement 5
Case 2
• Variable neither in global scope nor in local scope.
def avg(x, y, z):
s = (x+y+z)/3 # Statement 3
print(P1) # variable P1 is not a global variable, nor a local variable , so it will
# produce an error. Name P1 not defined
return s # statement 4
N1 = 10 # statement 1
N2 = 20 # Statement 2
N3 = 30 # statement 3
Res= avg( N1,N2,N3) # statement 4
print(Res) # statement 5
Case 3
• Variable name in local scope as well as in global scope.
def avg(x, y, z):
N1 = 40 # local variable N1 created with same name as global variable N1
s = (x+y+z)/3 # Statement 3
print(N1) # variable N1 is a local variable , so it will print 40
return s # statement 4
N1 = 10 # statement 1
N2 = 20 # Statement 2
N3 = 30 # statement 3
print(N1)
Res= avg( N1,N2,N3) # statement 4
print(Res) # statement 5
OUTPUT
10
40
60 Note: if a local var created with same name as that of global var, then it
hides the global variable
Case 4
• Variable name in local scope as well as in global scope, and
we want to use global variable
• def avg(x, y, z):
global N1 # this will tell the function avg() for N1 do not created a local var
N1 = 40 # variable N1 is global, and global variable N1 becomes 40 now
s = (x+y+z)/3
print(N1) # variable N1 is a local variable , so it will print 40
N1 = 10
N2 = 20
N3 = 30
print(N1)
avg( N1,N2,N3)
print(N1)
OUTPUT
10
40
40 Note: if a local var created with same name as that of global var, then it
hides the global variable
Mutable /Immutable properties of passed data
• Python variables are not storage containers, rather
they are memory references, they refer to the
memory address where the value is stored
• Depending upon the mutability/immutability of its
data type, a variable behaves differently.
• If a variable is referring to an immutable type then
any change in its value will also change its memory
address.
• But If a variable is referring to a mutable type then
any change in its value will not change its memory
address.
Mutable /Immutable properties of passed data
Thanks..
That’s All for today
Prepared by:
S K Kothiyal
PGT Comp Science
KV Pithoragarh