0% found this document useful (0 votes)
2 views17 pages

Functions3.3.2 Variable Scope

The document discusses the scope of variables in Python, detailing global and local scopes, and the lifetime of variables. It explains how global variables are accessible throughout the program while local variables are confined to their respective functions. Additionally, it covers name resolution rules and the mutable/immutable properties of data types in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Functions3.3.2 Variable Scope

The document discusses the scope of variables in Python, detailing global and local scopes, and the lifetime of variables. It explains how global variables are accessible throughout the program while local variables are confined to their respective functions. Additionally, it covers name resolution rules and the mutable/immutable properties of data types in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Class: XII

(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

• In Python, the term SCOPE refers to part(s) of


program within which a name is legal &
accessible.
• There are broadly 2 kinds of scope in Python:
– GLOBAL Scope
– LOCAL Scope
GLOBAL Scope

• A name declared in top level segment


(_main_) of a program is said to have a global
scope and is usable inside the whole program
and all blocks (functions, other blocks)
contained within the program.
• Any variable defined in the main program
section, is said to have a global scope .
GLOBAL variables
• Variables defined outside all functions are global
variables..
X = 5 # here X defined above all functions , it is also a global variable
along with Y & Z (declared in main segment)
def func(a) :
b = a+2
return b

Y =20 # Y is Global
Z = Y + func(X) # Z is Global
Print(Z)
LOCAL Scope

• A name declared in the function-body is said


to have a local scope, i.e. it can be used within
this function & other blocks contained under
it. The names of formal arguments also have
local scope.
• Local variable is a variable defined with in a
function. Such variable are said to have local
scope.
LOCAL variables
• Variables defined inside functions are local variables
X = 5 # here X defined above all functions , it is also a global variable
along with Y & Z
def func(a) :
b = a+2 # here a & b are local variable
return b

Y =20 # here Y is global variable


Z = Y + func(x) # here Z is global variable
print(Z)
Life Time of a Variable
• The life time of variable is time for which a variable lives in memory.
• For Global variable’s life time is entire program run (i.e. they live in
memory as long as the program is running)
• For local variables, life time is their function’s run (i.e. as long as their
function is being executed)
def func(a) :
b = a+2 # here a & b are local variable
return b

Y =20 # here Y & Z are Global Variable


Z = Y + func(x)
print(Z)

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

4. def avg(x, y, z):


5. sm = calsum(x,y,z) # Statement 3
6. return sm/3 # statement 4

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

You might also like