我一直在python獲得非綁定本地錯誤與下面的代碼:非綁定本地錯誤
xml=[]
global currentTok
currentTok=0
def demand(s):
if tokenObjects[currentTok+1].category==s:
currentTok+=1
return tokenObjects[currentTok]
else:
raise Exception("Incorrect type")
def compileExpression():
xml.append("<expression>")
xml.append(compileTerm(currentTok))
print currentTok
while currentTok<len(tokenObjects) and tokenObjects[currentTok].symbol in op:
xml.append(tokenObjects[currentTok].printTok())
currentTok+=1
print currentTok
xml.append(compileTerm(currentTok))
xml.append("</expression>")
def compileTerm():
string="<term>"
category=tokenObjects[currentTok].category
if category=="integerConstant" or category=="stringConstant" or category=="identifier":
string+=tokenObjects[currentTok].printTok()
currentTok+=1
string+="</term>"
return string
compileExpression()
print xml
下面是我得到確切的錯誤:
UnboundLocalError: local variable 'currentTok' referenced before assignment.
這是沒有意義的,我因爲我明確將currentTok
初始化爲我的代碼的第一行,並且我甚至將其標記爲global
以保證安全,並確保它在我所有方法的範圍內。
看看http://stackoverflow.com/questions/929777/why-does-assigning-to-my-global-variables-not-work-in-python特別是接受的答案。 – Ketouem 2013-02-26 16:53:00