python全局变量警告

2017-01-10 10:04:56来源:oschina作者:python-yzw人点击

第七城市

SyntaxWarning: name 'spam' is assigned to before globaldeclaration

例:


x = 0
def func(a, b, c):
if a == b:
global x
x = 10
elif b == c:
global x
x = 20

If you run this in a recent version of Python, the compiler will issue aSyntaxWarningpointing to the beginning of thefuncfunction.


Here’s the right way to write this:


x = 0
def func(a, b, c):
global x # <- here
if a == b:
x = 10
elif b == c:
x = 20

修改全局变量在函数改变修改前定义即可

第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台