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
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.
© 2025 OneMinuteCode. All rights reserved.