[Python-Dev] Whatever happened to 'nonlocal x = y'?
Guido van Rossum
guido at python.org
Fri Jan 5 10:47:21 EST 2018
On Fri, Jan 5, 2018 at 1:57 AM, Nathaniel Smith <njs at pobox.com> wrote:
> PEP 3104 says:
>
> """
> A shorthand form is also permitted, in which nonlocal is prepended to
> an assignment or augmented assignment:
>
> nonlocal x = 3
>
> The above has exactly the same meaning as nonlocal x; x = 3. (Guido
> supports a similar form of the global statement [24].)
> """
>
> The PEP metadata says it was approved and implemented in 3.0, yet this
> part never seems to have been implemented:
>
> Python 3.7.0a3+ (heads/master:53f9135667, Dec 29 2017, 19:08:19)
> [GCC 7.2.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> def f():
> ... x = 1
> ... def g():
> ... nonlocal x = 2
> File "<stdin>", line 4
> nonlocal x = 2
> ^
> SyntaxError: invalid syntax
>
> Was this just an oversight, or did it get rejected at some point and
> no-one remembered to update that PEP?
>
I don't recall (though someone with more time might find the discussion in
the archives or on the tracker). It was never implemented and I think it
shouldn't be. So we might as well update the PEP. It wouldn't be
particularly useful, since (by definition) the function that declares the
nonlocal variable is not its owner, and hence it's unlikely to make sense
to initialize it here. The same reasoning applies to global BTW.
--
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180105/cc29d785/attachment-0001.html>
More information about the Python-Dev
mailing list