Is there a way for the Mail class to inherit the Program class, except for the way the Program instance is passed to Mail ? Something other options do not work for me.

 from kivy.app import App class Mail(object): def __init__(self, program): self.pr = program def get_var(self): print self.pr.var class Program(App): def __init__(self, **kvargs): super(Program, self).__init__(**kvargs) self.var = True b= Mail(self) b.get_var() program = Program() 
  • And class Mail(Program): will not work? - Alexey Shimansky
  • Mail knows nothing about the Program. I cannot specify a parent in this way, so it will cause an error. - Xyanight
  • What prevents to swap Mail and Program ads? - user194374
  • Then Program will not know anything about Mail. - Xyanight

2 answers 2

Due to the fact that Python is a dynamic programming language, nothing terrible will happen if you use the Mail class inside the Program class and the Mail class has not yet been created.

Therefore, you can really just swap the declarations of one and another class:

 from kivy.app import App class Program(App): def __init__(self, **kvargs): super(Program, self).__init__(**kvargs) self.var = True b = Mail(self) b.get_var() class Mail(Program): def __init__(self, program): self.pr = program def get_var(self): print self.pr.var program = Program() 

When the Program class is declared, the App class will be required, which is used in the parent declaration.

At the time of the declaration of the Mail class, the presence of the Program class will be required, which will already be available in this area in scope.

     from kivy.app import App class Mail(object): def __init__(self): self.x() def get_var(self): print self.var class Program(Mail, App): def __init__(self, **kvargs): super(Program, self).__init__() self.var = True self.get_var() def x(self): print('I function x class Program') program = Program()