Python 3 removes name binding from outer scope

eryk sun eryksun at gmail.com
Tue Jul 25 14:36:54 EDT 2017


On Tue, Jul 25, 2017 at 8:43 AM, Chris Angelico <rosuav at gmail.com> wrote:
>
> I'm not actually sure what happens if you use a global declaration at
> top level. Is it ignored? Is it an error?

It isn't ignored, but it shouldn't make a difference since normally at
module level locals and globals are the same. It makes a difference in
an exec() that uses separate locals and globals dicts. For example:

    >>> exec('print(x)', {'x':'G'}, {'x':'L'})
    L
    >>> exec('global x; print(x)', {'x':'G'}, {'x':'L'})
    G



More information about the Python-list mailing list