From 9f05f98730bbc36f4ad173845458827c4df879f6 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 21 Aug 2025 14:58:38 +0200 Subject: [PATCH 1/2] gh-137376: Add note on top-level `global` declarations (GH-137707) 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 2a72af4e9a3299..3f2bcb2a60ee78 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -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 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 From 339f5da6395868073e22157424405b89cafb5c6d Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Thu, 21 Aug 2025 11:02:29 -0400 Subject: [PATCH 2/2] gh-138011: Clarify tutorial method object example code (#138014) x must be a MyClass instance for examples to work. --------- Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/tutorial/classes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index fa964271d79bd8..9ab003d5cd3dd5 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -359,7 +359,7 @@ Usually, a method is called right after it is bound:: x.f() -In the :class:`!MyClass` example, this will return the string ``'hello world'``. +If ``x = MyClass()``, as above, this will return the string ``'hello world'``. However, it is not necessary to call a method right away: ``x.f`` is a method object, and can be stored away and called at a later time. For example::