https://github.com/python/cpython/commit/2d24b13d034e3097a49e8be97188bd86698de74d commit: 2d24b13d034e3097a49e8be97188bd86698de74d branch: 3.13 author: Miss Islington (bot) <[email protected]> committer: Yhg1s <[email protected]> date: 2025-09-18T13:02:17+01:00 summary:
[3.13] gh-137376: Add note on top-level `global` declarations (GH-137707) (#138022) (cherry picked from commit 9f05f98730bbc36f4ad173845458827c4df879f6) Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Brian Schubert <[email protected]> files: M Doc/reference/simple_stmts.rst diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index 165436eaa327b4..5a9c398b54ca92 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -972,10 +972,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 _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
