From: http://blog.sina.com.cn/s/blog_8d3652760101d01p.html
一、意思:
二、错误原因
三、产生这个错误的场景
参考资料:http://www.uplook.cn/biancheng/107/1078875/
python代码:
val=9
def test(flag):
if flag:
val = 1
else:
print 'fuck'
return val
test(0)
错误提示:UnboundLocalError: local variable 'val' referenced before assignment
解决方法:用global关键字来进行说明该变量是全局变量
python代码:
val=9
def test(flag):
global val
if flag:
val = 1
else:
print 'test'
return val
test(0)
val=9
def
test(0)
错误提示:UnboundLocalError: local variable 'val' referenced before assignment
解决方法:用global关键字来进行说明该变量是全局变量
python代码:
val=9
def test(flag):
test(0)