Python错误 SyntaxWarning: name 'xxx' is assigned to before global declaration

假装没事ソ 提交于 2020-08-09 07:10:00

The most common reason for this error is that you’re using multiple global declarations in the same function. Consider this example:

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 a SyntaxWarning pointing to the beginning of the func function.






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




标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!