运行此代码:
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
?