I have two files, both of which define a class.
project/
__init__.py
thing.py
thinglist.py
thing.py defines a class Thing, which can output lists of other Things:
# thing.py
from project.thinglist import ThingList
class Thing:
def make_more_things(self):
return ThingList(Thing(), Thing())
A ThingList is a list of Thing objects, which extends list with some other functions sprinkled in:
# thinglist.py
from project.thing import Thing
class ThingList(list):
def __init__(self, *args):
list.__init__(self, *args)
assert all([isinstance(t, Thing) for t in self])
def some_other_method: pass
Instantly we have a problem: a circular dependency. Thing needs ThingList in order to construct ThingLists. ThingList needs Thing in order to verify that it is made of and only of Things. But Python won't let me import Thing into thinglist.py.
To get around this, I could put both classes into the same file. But they're both pretty lengthy and for the sake of my own sanity I really don't want to do that. I could also just omit the isinstance check, but then I might be getting errors down the line that could easily be solved right now.
How can I avoid this circular dependency without combining the two files?