[Python-ideas] Unify global and nonlocal

Nick Coghlan ncoghlan at gmail.com
Mon Feb 24 22:40:05 CET 2014


On 25 Feb 2014 04:18, "Manuel Cerón" <ceronman at gmail.com> wrote:
>
> On Mon, Feb 24, 2014 at 4:22 AM, Saket Dandawate <newton3143 at gmail.com>
wrote:
> > Also why can't it be like this too
> >>>> global x = 3
> >
> > rather than only
> >>>> global x
> >>>> x=3
>
> I can't find the discussion thread, but originally PEP3104 proposed
> this kind of syntax for nonlocal, but it was never actually
> implemented because of ambiguity with the unpacking assignment case:
>
> nonlocal x, y, z = 1, 2, 3
>
> Does this mean that x, y and z are non locals? or just x?
>
> Now I understand the differences between nonlocal and global. But
> still is not very clear for me why nonlocal can't access the global
> module namespace as well, not to be used in the same way as global,
> but to access names already defined in the global namespace.

Because functions are compiled as a unit, allowing nonlocal references to
be checked at compile time, but globals are resolved lazily and hence
aren't checked until runtime.

There's no way to flip a reference from a closure variable to a global
dynamically, so the compiler treats an explicit closure reference (i.e. a
nonlocal declaration) to a name that doesn't exist as an error (either the
variable name is wrong or the user meant to write global rather than
nonlocal).

Cheers,
Nick.

>
> Manuel.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140225/16139c7e/attachment.html>


More information about the Python-ideas mailing list