Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Calling a variable exists in 3 classes

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading