Home > Software engineering >  Python - Calling a variable exists in 3 classes
Python - Calling a variable exists in 3 classes

Time:08-11

I have a project with 3 classes, I will represent them as in the code below, in the first it will run class 1, and it must change the value of class 2, and when calling the variable in class 3 you should read the value we put in class 1...

but the code I made did not change That value

class class1(QMainWindow):
    def __init__(self):
        self.models = class2()
        self.models.variable = 200



class class2(QWidget):
    variable = 0
    def __init__(self):
        super().__init__()



class class3:
    def __init__(self):
        super().__init__()
        self.models = class2()
        print(self.models.variable) # Here I want to show 200 but the value that appears is 0
  

There must be 3 classes as in the order, also class 1 must not be called in class 3

CodePudding user response:

It sounds like you want to make class2 as a namespace to hold variable. So just do that:

class class1(QMainWindow):
    def __init__(self):
        self.models = class2()
        class2.variable = 200



class class2(QWidget):
    variable = 0
    def __init__(self):
        super().__init__()



class class3:
    def __init__(self):
        super().__init__()
        self.models = class2()
        print(class2.variable)
  • Related