Use of statement 'global' in scripts.
Greg Ewing
greg.ewing at canterbury.ac.nz
Wed May 8 04:56:00 EDT 2024
On 8/05/24 1:32 pm, Popov, Dmitry Yu wrote:
> The statement 'global', indicating variables living in the global scope, is very suitable to be used in modules. I'm wondering whether in scripts, running at the top-level invocation of the interpreter, statement 'global' is used exactly the same way as in modules?
The 'global' statement declares a name to be module-level, so there's no
reason to use it at the top level of either a script or a module, since
everything there is module-level anyway.
You only need it if you want to assign to a module-level name from
within a function, e.g.
spam = 17
def f():
global spam
spam = 42
f()
# spam is now 42
A script is a module, so everything that applies to modules also
applies to scripts.
--
Greg
More information about the Python-list
mailing list