mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.13] gh-137376: Add note on top-level global declarations (GH-137707) (#138022)
(cherry picked from commit 9f05f98730)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
This commit is contained in:
parent
df349036b3
commit
2d24b13d03
1 changed files with 9 additions and 2 deletions
|
|
@ -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
|
:keyword:`!global`, although free variables may refer to globals without being
|
||||||
declared global.
|
declared global.
|
||||||
|
|
||||||
The :keyword:`global` statement applies to the entire scope of a function or
|
The :keyword:`!global` statement applies to the entire current scope
|
||||||
class body. A :exc:`SyntaxError` is raised if a variable is used or
|
(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.
|
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::
|
.. index::
|
||||||
pair: built-in function; exec
|
pair: built-in function; exec
|
||||||
pair: built-in function; eval
|
pair: built-in function; eval
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue