此代码给出了错误:UnboundLocalError: local variable 'LINES' referenced before assignment
但LINES
显然已初始化,因为如果我注释掉 print 语句下方的行,它不会抛出任何错误并按 len(lines) = 0
预期打印。我对python不了解吗?这里发生了什么?
LINES = []
def foo():
for prob in range(1,3):
print "len(lines) = %d" % len(LINES)
LINES = []
if __name__ == "__main__":
foo()