Python 3.0 nonlocal statement

C

Casey

In PEP 3104 the nonlocal statement was proposed and accepted for
implementation in Python 3.0 for access to names in outer scopes. The
proposed syntax included an optional assignment or augmented
assignment to the outer name, such as:

nonlocal x += 1

This syntax doesn't appear to be supported in the 3.0 implementation.
My question is: was this intentional or was it missed in the initial
release? If it was intentional, is there any plan to support it in a
later 3.x release? I realize it is a very small convenience feature
but I have already come across a couple of cases where I use nested
functions where it does make the code seem a little cleaner.

Regards, Casey
 
M

Matimus

In PEP 3104 the nonlocal statement was proposed and accepted for
implementation in Python 3.0 for access to names in outer scopes.  The
proposed syntax included an optional assignment or augmented
assignment to the outer name, such as:

nonlocal x += 1

This syntax doesn't appear to be supported in the 3.0 implementation.
My question is: was this intentional or was it missed in the initial
release?  If it was intentional, is there any plan to support it in a
later 3.x release?  I realize it is a very small convenience feature
but I have already come across a couple of cases where I use nested
functions where it does make the code seem a little cleaner.

Regards, Casey

`nonlocal` should behave just like `global` does. It doesn't support
that syntax either. So, yes it was intentional. No, there probably is
no plan to support it in a later release.

Matt
 
C

Casey

`nonlocal` should behave just like `global` does. It doesn't support
that syntax either. So, yes it was intentional. No, there probably is
no plan to support it in a later release.

Matt

From my perspective, that's an unfortunate decision and I question the
rationale. First, nonlocal doesn't behave exactly like global (you
cannot declare a previously undefined variable nonlocal as you can
with global). Second, the PEP 3104 description explicitly mentions
that Guido favored adding this behavior to global; I would have
preferred that approach to not providing the extended assignment
support for nonlocal. Third, I believe that it adds some clarity to
the code. Seeing 'nonlocal x += 1' immediately tells me that I'm
incrementing a variable defined in an outer scope. Having 'nonlocal
x' on one line and 'x += 1' on another makes it a little less clear.

But I do appreciate the reply!

Casey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top