Thank you!
From: Python-list on behalf of
Greg Ewing via Python-list
Sent: Wednesday, May 8, 2024 3:56 AM
To: python-list@python.org
Subject: Re: Use of statement 'global' in scripts.
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,
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
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
--
https://urldefense.us/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!G_uCfscf7eWS!airWCCS1QeLAhk0AfN3VxhuV9MZkx8YBhs5Vjf89K2WZPjhCUkXt9culFzwlX1_ON0G17lukcR79-kWAsA$
--
https://mail.python.org/mailman/listinfo/python-list