IDLE being too clever checking nonlocal declarations?

Chris Angelico rosuav at gmail.com
Tue Oct 22 02:24:07 EDT 2013


On Tue, Oct 22, 2013 at 4:57 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Yep, I got that, but what I'm saying is that it is too strict to raise
> the exception at the point where it sees "nonlocal q". The CPython
> interpreter allows q to be defined inside function a but after function
> b, e.g. this is allowed:
>
> def a():
>     def b():
>         nonlocal q
>         q += 1
>     q = 2  # <=======
>
>
> If IDLE and the code.py module requires q to be strictly defined before
> function b, then it is too strict. Your analysis of the bug as being in
> code.py seems plausible.

Yeah. I came across this as I was knocking together a test for
something else, and my work-around was to define the inner function as
just "pass", and then go back and edit in the nonlocal declaration
after adding the assignment outside. The only problem is that it's
syntax-checking something that's only half-entered, and is bombing on
it - thus stopping you from typing in the bit that would make it
valid.

Thanks Terry for the extra info on the tracker issue.

ChrisA



More information about the Python-list mailing list