def sub1(): a = 4 y = 5 z = 6 sub2() print("In sub1,") print('x = %d' %x) print('y = %d' %y) print('z = %d' %z) print('a = %d' %a) def sub2(): a = 7 b = 8 z = 9 sub3() print("In sub2,") print('x = %d' %x) print('y = %d' %y) print('z = %d' %z) print('a = %d' %a) print('b = %d' %b) def sub3(): a = 10 x = 11 w = 12 print("In sub3,") print('x = %d' %x) print('y = %d' %y) print('z = %d' %z) print('a = %d' %a) print('w = %d' %w) x = 1 y = 2 z = 3 sub1() print("In main program,") print('x = %d' %x) print('y = %d' %y) print('z = %d' %z) |
The Python uses the static scoping rule.The parents of sub1, sub2,and sub3 are outside of them. Output: In sub3,
x = 11
y = 2
z = 3
a = 10
w = 12
In sub2,
x = 1
y = 2
z = 9
a = 7
b = 8
In sub1,
x = 1
y = 5
z = 6
a = 4
In main program,
x = 1
y = 2
z = 3
Why does it print start from sub3?
|
If Python changed to dynamic scoping rule,what will be output? ![]() Examples of languages that use dynamic scope include Logo, Emacs Lisp, LaTeX and the shell languages bash, dash, and PowerShell. Dynamic scope is fairly easy to implement (compiler). |