This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steve.dower
Recipients Mahdi Jafary, paul.moore, steve.dower, tim.golden, xtreak, zach.ware
Date 2019-06-11.15:55:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1560268510.71.0.971514532694.issue37234@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-06-11 15:55:10steve.dowersetrecipients: + steve.dower, paul.moore, tim.golden, zach.ware, xtreak, Mahdi Jafary
2019-06-11 15:55:10steve.dowersetmessageid: <1560268510.71.0.971514532694.issue37234@roundup.psfhosted.org>
2019-06-11 15:55:10steve.dowerlinkissue37234 messages
2019-06-11 15:55:10steve.dowercreate