The __init__
method executes when you create an instance of the class. In this case, you should get it to work by simply changing the import statements like this:
import a
class B:
def __init__(self):
self.a = a.A(self)
import b
class A:
def __init__(self, ref):
assert isinstance(ref, b.B)
self.ref = ref