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.

classification
Title: UnboundLocalError in nested function
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Andreas Hofmeister
Priority: normal Keywords:

Created on 2010-06-21 19:46 by Andreas Hofmeister, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg108307 - (view) Author: Andreas Hofmeister (Andreas Hofmeister) Date: 2010-06-21 19:46
Description:
An unexpected UnboundLocalError is produced when assigning a value to a variable inside a nested function. The first assignment to the variable is in the enclosing function.

Example:
def x():
     a = False
     def y():
         print a
         a = True
     return y

Calling x()() produces an UnboundLocalError on the 'print a' line.
If the 'a = True' line is removed, no error occurs.

Tested with:
  - 2.5.1
  - 2.6.5

Keywords: 
Nested function, UnboundLocalError, variable assignment

Thank you for your attention
msg108308 - (view) Author: Andreas Hofmeister (Andreas Hofmeister) Date: 2010-06-21 19:51
Duplicate of 9049.

Sorry for the inconvenience.
History
Date User Action Args
2022-04-11 14:57:02adminsetgithub: 53296
2010-06-21 19:52:02eric.araujosetresolution: duplicate
stage: resolved
2010-06-21 19:51:25Andreas Hofmeistersetstatus: open -> closed

messages: + msg108308
2010-06-21 19:46:39Andreas Hofmeistercreate