python how to change the global variables

This question already has an answer here:

  • Using global variables in a function other than the one that created them 18 answers

  • 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.


    您需要将global语句放在要访问全局变量的范围内,即:

    upload_confirm = False
    
    def confirm_upload():
        global upload_confirm
        upload_confirm = True
    

    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.

    链接地址: http://www.djcxy.com/p/23892.html

    上一篇: 赋值前引用局部变量“第一个”

    下一篇: python如何更改全局变量