Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aa643c

Browse files
committed
Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
1 parent 7606e4d commit 9aa643c

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lib/test/output/test_scope

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ test_scope
1313
12. lambdas
1414
13. UnboundLocal
1515
14. complex definitions
16+
15. scope of global statements

Lib/test/test_scope.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,68 @@ def addPair((c, d)):
318318
return addPair
319319

320320
verify(makeAddPair((1, 2))((100, 200)) == (101,202))
321+
322+
print "15. scope of global statements"
323+
# Examples posted by Samuele Pedroni to python-dev on 3/1/2001
324+
325+
# I
326+
x = 7
327+
def f():
328+
x = 1
329+
def g():
330+
global x
331+
def i():
332+
def h():
333+
return x
334+
return h()
335+
return i()
336+
return g()
337+
verify(f() == 7)
338+
verify(x == 7)
339+
340+
# II
341+
x = 7
342+
def f():
343+
x = 1
344+
def g():
345+
x = 2
346+
def i():
347+
def h():
348+
return x
349+
return h()
350+
return i()
351+
return g()
352+
verify(f() == 2)
353+
verify(x == 7)
354+
355+
# III
356+
x = 7
357+
def f():
358+
x = 1
359+
def g():
360+
global x
361+
x = 2
362+
def i():
363+
def h():
364+
return x
365+
return h()
366+
return i()
367+
return g()
368+
verify(f() == 2)
369+
verify(x == 2)
370+
371+
# IV
372+
x = 7
373+
def f():
374+
x = 3
375+
def g():
376+
global x
377+
x = 2
378+
def i():
379+
def h():
380+
return x
381+
return h()
382+
return i()
383+
return g()
384+
verify(f() == 2)
385+
verify(x == 2)

0 commit comments

Comments
 (0)