]> granicus.if.org Git - python/commitdiff
Test interaction of global and nested scopes -- thanks to Samuele Pedroni.
authorGuido van Rossum <guido@python.org>
Thu, 1 Mar 2001 20:35:45 +0000 (20:35 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 1 Mar 2001 20:35:45 +0000 (20:35 +0000)
Lib/test/output/test_scope
Lib/test/test_scope.py

index 0535e2664fe1b0454ce70ea993c90d3d43ea9a6e..5d41e8c384e1240722f30365315af7a6387b76c6 100644 (file)
@@ -13,3 +13,4 @@ test_scope
 12. lambdas
 13. UnboundLocal
 14. complex definitions
+15. scope of global statements
index b4c492aa977dde6b356eca2dd021d20cfe25a657..0633b1433f944889d04a5c5de9f66b9a38fdfdc5 100644 (file)
@@ -318,3 +318,68 @@ def makeAddPair((a, b)):
     return addPair
 
 verify(makeAddPair((1, 2))((100, 200)) == (101,202))
+
+print "15. scope of global statements"
+# Examples posted by Samuele Pedroni to python-dev on 3/1/2001
+
+# I
+x = 7
+def f():
+    x = 1
+    def g():
+        global x
+        def i():
+            def h():
+                return x
+            return h()
+        return i()
+    return g()
+verify(f() == 7)
+verify(x == 7)
+
+# II
+x = 7
+def f():
+    x = 1
+    def g():
+        x = 2
+        def i():
+            def h():
+                return x
+            return h()
+        return i()
+    return g()
+verify(f() == 2)
+verify(x == 7)
+
+# III
+x = 7
+def f():
+    x = 1
+    def g():
+        global x
+        x = 2
+        def i():
+            def h():
+                return x
+            return h()
+        return i()
+    return g()
+verify(f() == 2)
+verify(x == 2)
+
+# IV
+x = 7
+def f():
+    x = 3
+    def g():
+        global x
+        x = 2
+        def i():
+            def h():
+                return x
+            return h()
+        return i()
+    return g()
+verify(f() == 2)
+verify(x == 2)