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 on simple in-place assignment of an inner scope
Type: Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Janssen, r.david.murray
Priority: normal Keywords:

Created on 2012-08-11 00:19 by Mark.Janssen, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg167928 - (view) Author: zipher (Mark.Janssen) Date: 2012-08-11 00:19
>>> num = 1
>>> def t1():
      print num
>>> t1()
1
>>> def t2():
...   num+=1
...   print num
>>> t2()
UnboundLocalError: local variable 'num' referenced before assignment

It seems num is bound in t1, but not t2, even though they are the same scope.  Am I missing something?
msg167929 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-11 01:01
In t2, you assign to num.  That makes it local.  In t1, you don't, so num is picked up from the global scope.
History
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59826
2012-08-11 01:01:08r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg167929

resolution: not a bug
stage: resolved
2012-08-11 00:19:20Mark.Janssencreate