file separation, class instance sharing

Asked 2 years ago, Updated 2 years ago, 77 views

a.py

from b import *

 class A(): 
     def __init__(self): 
         super().__init__() 
         self.a = 1 
         self.b = B() 
         self.c = self.b.sum()

b.py

from a import  *

 class B(): 
     def __init__(self): 
         super().__init__() 
         self.a = A() 

     def sum(self): 
         s = self.a.a + 1 
         return s

You can use an instance of class A of b.py from a.py (unidirectional) if you did not import self.b=B() or self.c = self.b.sum() from b.py

The instance variable of class A in a.py is b.Bring it to py, use it, and also b.The instance variable of class B in py is a.If I try to import it into py and use it (two-way), the error keeps saying that the file could not be found.

How do I organize this?

python class

2022-09-20 19:07

1 Answers

It's not supposed to be two-way. If you do it in one direction, but define a new one, you can implement it in a similar way in both directions.


2022-09-20 19:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.