Python 错误:SyntaxError: name 'img_exclude' is assigned to before global declaration
这个错误是因为你在全局作用域中使用了 'global' 关键字,但是在它之前已经对 'img_exclude' 进行了赋值操作。
例如:
img_exclude = []
def my_function():
global img_exclude
img_exclude.append('image1.jpg')
my_function()
在这个例子中,'img_exclude' 被赋值为空列表。然后在 'my_function()' 中,我们使用了 'global' 关键字来声明 'img_exclude' 是一个全局变量。但是,这个声明在 'img_exclude' 被赋值之后,所以会产生 'SyntaxError: name 'img_exclude' is assigned to before global declaration' 错误。
要解决这个问题,可以将 'global' 声明放在赋值操作之前,或者使用另一个变量名来避免这个问题。例如:
img_exclude = []
def my_function():
global img_exclude
img_exclude = ['image1.jpg']
my_function()
或者:
img_exclude = []
def my_function():
global img_exclude_new
img_exclude_new = ['image1.jpg']
my_function()
img_exclude = img_exclude_new
原文地址: https://www.cveoy.top/t/topic/nuaH 著作权归作者所有。请勿转载和采集!