運行此代碼:蟒蛇本地範圍
import re
regex = re.compile("hello")
number = 0
def test():
if regex.match("hello"):
number += 1
test()
產生以下錯誤:
Traceback (most recent call last):
File "test.py", line 12, in <module>
test()
File "test.py", line 10, in test
number += 1
UnboundLocalError: local variable 'number' referenced before assignment
我爲什麼可以參考regex
從裏面的函數,而不是number
?
你還應該閱讀python [FAQ](http://docs.python.org/2/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has這個確切的問題已經被回答了。如果你想知道更多[這裏](http://eli.thegreenplace.net/2011/05/15/understanding-unboundlocalerror-in-python/)這篇文章。 – root 2013-02-15 06:18:45