From b3affab3873d432042f84e4f1b7cc6ad85494207 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 21 Aug 2025 14:58:38 +0200 Subject: [PATCH] gh-137376: Add note on top-level `global` declarations (GH-137707) (cherry picked from commit 9f05f98730bbc36f4ad173845458827c4df879f6) Co-authored-by: Petr Viktorin Co-authored-by: Brian Schubert --- Doc/reference/simple_stmts.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index 5e3ede69d1e5d7..3d046e9f088f81 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -969,10 +969,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