Issue 15621: UnboundLocalError on simple in-place assignment of an inner scope (original) (raw)

Issue15621

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:34 admin set github: 59826
2012-08-11 01:01:08 r.david.murray set status: open -> closednosy: + r.david.murraymessages: + resolution: not a bugstage: resolved
2012-08-11 00:19:20 Mark.Janssen create