Skip to content

gh-137376: Add note on top-level global declarations #137707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Doc/reference/simple_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -971,10 +971,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 class definition).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(module, function body or class class definition).
(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
Expand Down
Loading