• Use of statement 'global' in scripts.

    From Popov, Dmitry Yu@[email protected] to comp.lang.python on Wed May 8 01:32:32 2024
    From Newsgroup: comp.lang.python

    Dear Sirs.
    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? If there are any differences, I would really appreciate any comments on this.
    Regards,
    Dmitry Popov
    Lemont, IL
    USA
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Greg Ewing@[email protected] to comp.lang.python on Wed May 8 20:56:00 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Popov, Dmitry Yu@[email protected] to comp.lang.python on Wed May 8 18:52:57 2024
    From Newsgroup: comp.lang.python

    Thank you!
    ________________________________
    From: Python-list <python-list-bounces+dpopov=[email protected]> on behalf of Greg Ewing via Python-list <[email protected]>
    Sent: Wednesday, May 8, 2024 3:56 AM
    To: [email protected] <[email protected]>
    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!airWCCS1QeLAhk0AfN3VxhuV9MZkx80000YBhs5Vjf89K2WZPjhCUkXt9culFzwlX1_ON0G17lukcR79-kWAsA$

    --- Synchronet 3.20a-Linux NewsLink 1.114