[issue37234] error in variable

Steve Dower report at bugs.python.org
Tue Jun 11 11:55:10 EDT 2019


Steve Dower <steve.dower at python.org> added the comment:

This is intended behavior.

When a variable has an assignment anywhere in a function, it becomes a local. Once a local variable exists, it will shadow the non-local variable.

So by having the "n =", you are marking "n" as a local variable. When the "n + 1" tries to read the local, it has not yet been set, so it fails.

If you want to read and write to "n" in the outer function, you will need the nonlocal statement - https://docs.python.org/3/reference/simple_stmts.html#the-nonlocal-statement

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37234>
_______________________________________


More information about the Python-bugs-list mailing list