Make "global after use" a SyntaxError

Hi all, The documentation at https://docs.python.org/3/reference/simple_stmts.html says that: "Names listed in a global statement must not be used in the same code block textually preceding that global statement" But then later: "CPython implementation detail: The current implementation does not enforce the two restrictions, but programs should not abuse this freedom, as future implementations may enforce them..." Code like this def f(): x = 1 global x gives SyntaxWarning for several releases, maybe it is time to make it a SyntaxError? (I have opened an issue for this http://bugs.python.org/issue27999 I will submit a patch soon). -- Ivan

I don't know if feedback from a single, humble Python programmer is of any value, but: +1 I do sometimes have global statements at the start of the bit of code to which they apply (rather than having all global statements agglomerated at the start of the function they are in). This seems to me consistent with good practice, whether for clarity or to make code cut-and-pasting easier. I cannot imagine ever wanting a global statement to be AFTER the first reference to one of the global variables it mentions. Best wishes. Rob Cliffe On 07/09/2016 17:59, Guido van Rossum wrote:

I don't know if feedback from a single, humble Python programmer is of any value, but: +1 I do sometimes have global statements at the start of the bit of code to which they apply (rather than having all global statements agglomerated at the start of the function they are in). This seems to me consistent with good practice, whether for clarity or to make code cut-and-pasting easier. I cannot imagine ever wanting a global statement to be AFTER the first reference to one of the global variables it mentions. Best wishes. Rob Cliffe On 07/09/2016 17:59, Guido van Rossum wrote:
participants (3)
-
Guido van Rossum
-
Ivan Levkivskyi
-
Rob Cliffe