]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-137376: Add note on top-level `global` declarations (GH-137707) (GH-138023)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 7 Oct 2025 17:59:29 +0000 (19:59 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 17:59:29 +0000 (19:59 +0200)
(cherry picked from commit 9f05f98730bbc36f4ad173845458827c4df879f6)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Doc/reference/simple_stmts.rst

index d674804df8f82efdd43354ee18dd98d735efba1c..9c022570e7e8478dcd8ebf59dbbdf3620eafd714 100644 (file)
@@ -974,10 +974,17 @@ as globals. It would be impossible to assign to a global variable without
 :keyword:`!global`, although free variables may refer to globals without being
 declared global.
 
-The :keyword:`global` statement applies to the entire scope of a function or
-class body. A :exc:`SyntaxError` is raised if a variable is used or
+The :keyword:`!global` statement applies to the entire current scope
+(module, function body or class definition).
+A :exc:`SyntaxError` is raised if a variable is used or
 assigned to prior to its global declaration in the scope.
 
+At the module level, all variables are global, so a :keyword:`!global`
+statement has no effect.
+However, variables must still not be used or
+assigned to prior to their :keyword:`!global` declaration.
+This requirement is relaxed in the interactive prompt (:term:`REPL`).
+
 .. index::
    pair: built-in function; exec
    pair: built-in function; eval