Steve Dower <steve.do...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue37234>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to