my global can't change in the def of a class

余昌翰

I define a varible, name 'Decisoin_name' and set -1 at first

and I try to change it in a def of a class

because I’d like to add 1 everytime when I call the def

but the system send me a message

"local variable 'Decision_name' referenced before assignment"

what can I do?

Would you give me a solution to fix it ? thank you

The following is my code

Decision_name = -1

class Decision_Dialog(QDialog):

    def sendback(self):

        Decision_name+=1

        print(Decision_name)

        self.close()
heemayl

You can't change a global name from a class method directly, you need to declare it as a global variable beforehand:

class Decision_Dialog(QDialog):
    def sendback(self):
        global Decision_name
        Decision_name += 1

Although if it does not need to be a global variable, you can take different routes e.g. make it a class variable and let each instance modify to it's need, or make it a instance variable by defining in __init__ and make necessary changes afterwards.

Also, you should be using snake_case for variable names e.g. decision_name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related