a clarification on the "global" statement sought

eryk sun eryksun at gmail.com
Fri Mar 11 04:20:32 EST 2016


On Fri, Mar 11, 2016 at 2:13 AM, Charles T. Smith
<cts.private.yahoo at gmail.com> wrote:
> When might a "global" statement be used in the outermost level of a module?

You wouldn't need this in a normal module, because locals and globals
are the same. It may be useful if you're using exec with separate
locals and globals, and need to set a global. For example:

    source = 'global y; x, y = 1, 2'
    gvars, lvars = {}, {}
    exec(source, gvars, lvars)

    >>> lvars
    {'x': 1}
    >>> gvars['y']
    2

Probably you'll never need this.



More information about the Python-list mailing list