Python – Calling a variable exists in 3 classes

Question:

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

Answers:

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)
Answered By: quamrana
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.