About global declarations, Python Language Ref (PLR) explains: [https://docs.python.org/3.4/reference/simple_stmts.html#the-global-statement] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Names listed in a global statement must not be used in the same code block textually preceding that global statement. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What I understand is that the following code is incorrect: # --------------------------------------- def f(): x=42 global x f() print(x) # --------------------------------------- And indeed, executing this piece of code "raises" a warning : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test.py:3: SyntaxWarning: name 'x' is assigned to before global declaration global x 42 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Now, global declaration has another restriction, as PLR explains: [https://docs.python.org/3.4/reference/simple_stmts.html#the-global-statement] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Names listed in a global statement must not be defined as formal parameters or in a for loop control target, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ What I understand is that the following is a must-not-code: # --------------------------------------- def f(): global i for i in range(1,3): print(10*i) f() print(i) # --------------------------------------- But, the later code executes silently without any warning: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 20 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ So my question is: what is the restriction about global as loop control variable the docs is referring to? -- https://mail.python.org/mailman/listinfo/python-list