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

来源:互联网 发布:我的网络朋友圈作文 编辑:程序博客网 时间:2024/04/28 15:45

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

阅读全文
0 0
原创粉丝点击