Consider the following Python program x 1 y 3 z 5 def sub
Consider the following Python program:
x = 1;
y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
...
def sub2():
global x;
a = 13;
x = 15;
w = 17;
...
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
...
...
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assum- ing static scoping is used.
Solution
Here I am displaying all the accessible variable with units in each subroute and where they have declared. It is actually showing that which variable value is used in particular sub-routine.
In main:
x=1 Main
y=3 Main
z=5 Main
In sub1:
a=7 sub1
y=9 sub1
z=11 sub1
x=1 main
In sub2:
a=13 sub2
x=15 sub2
w=17 sub2
y=3 main
z=5 main
In sub3:
a=19 sub3
b= 21 sub3
z=23 sub3
w=17 sub2
x=15 sub2 //because of global variable
y =3 main

