3

I would like to get some kind of confirmation that the upload is success, I have my methods defined similar to the following. However the value of global variable is not changing. Please help

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()
Anup Singh
  • 313
  • 1
  • 5
  • 18

4 Answers4

3

You could try this:

def confirm_upload():
    global upload_confirm
    upload_confirm = True

Since you are doing upload_confirm = True in a local scope, Python treat it like a local variable. Hence, your global variable stays the same.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

You need to put the global statement in that scope where you want to access the global variable, i.e.:

upload_confirm = False

def confirm_upload():
    global upload_confirm
    upload_confirm = True
poke
  • 369,085
  • 72
  • 557
  • 602
1

Try this inside your confirm_upload() method.

def confirm_upload():
    global upload_confirm #Add this line
    upload_confirm = True

You need to declare it as global inside methods else it will be by default local.

venki421
  • 494
  • 1
  • 2
  • 8
0

global statement should be inside the function.

def confirm_upload():
    global upload_confirm
    upload_confirm = True

Otherwise, upload_confirm = .. will create a local variable.

falsetru
  • 357,413
  • 63
  • 732
  • 636