我想得到某种确认,即上传成功,我的方法定义类似于以下内容。但是,全局变量的值不会改变。请帮忙
global upload_confirm
upload_confirm = False
def confirm_upload():
upload_confirm = True
def start_new_upload():
confirm_upload()
while (upload_confirm != True):
print "waiting for upload to be true"
time.sleep(5)
if (upload_confirm == True):
print "start Upload"
start_new_upload()
答案 0 :(得分:3)
你可以试试这个:
def confirm_upload():
global upload_confirm
upload_confirm = True
由于您在本地范围内执行upload_confirm = True
,因此Python将其视为局部变量。因此,您的全局变量保持不变。
答案 1 :(得分:1)
您需要将global
语句放在要访问全局变量的范围内,即:
upload_confirm = False
def confirm_upload():
global upload_confirm
upload_confirm = True
答案 2 :(得分:1)
在confirm_upload()
方法中尝试此操作。
def confirm_upload():
global upload_confirm #Add this line
upload_confirm = True
你需要将它声明为全局内部方法,否则它将默认为local。
答案 3 :(得分:0)
global
语句应该在函数内部。
def confirm_upload():
global upload_confirm
upload_confirm = True
否则,upload_confirm = ..
将创建一个局部变量。