1# test implicit scoping rules 2 3# implicit nonlocal, with variable defined after closure 4def f(): 5 def g(): 6 return x # implicit nonlocal 7 x = 3 # variable defined after function that closes over it 8 return g 9print(f()()) 10 11# implicit nonlocal at inner level, with variable defined after closure 12def f(): 13 def g(): 14 def h(): 15 return x # implicit nonlocal 16 return h 17 x = 4 # variable defined after function that closes over it 18 return g 19print(f()()()) 20 21# local variable which should not be implicitly made nonlocal 22def f(): 23 x = 0 24 def g(): 25 x # local because next statement assigns to it 26 x = 1 27 g() 28try: 29 f() 30except NameError: 31 print('NameError') 32