Re: [Python-ideas] Unify global and nonlocal
On Mon, Feb 24, 2014 at 4:22 AM, Saket Dandawate <newton3143@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. Manuel.
On 25 Feb 2014 04:18, "Manuel Cerón" <ceronman@gmail.com> wrote:
On Mon, Feb 24, 2014 at 4:22 AM, Saket Dandawate <newton3143@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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
participants (2)
-
Manuel Cerón -
Nick Coghlan