I have a situation where there two related large python classes and hence i have put them in separate files. Let say classes are Cobra and Rat.
Now need to call methods of Rat from methods of Cobra and vice versa. For this i need to import Cobra in Rat.py and Rat in Cobra.py
This creates an import loop and gives an error. Cant import Cobra inside Cobra.
How to fix this??
Cobra.py:
import Rat
class Cobra():
def check_prey(self, rat ):
# Some logic
rat.foo()
Rat.py:
import Cobra
class Rat():
def check_predator(self, snake ):
# some_logic ..
snake.foo()
Rat
andCobra
classes, how about a common base classAnimal
? This way you can importAnimal
in bothRat
andCobra
. - user2032433