class A:
    def __init__(self, txt: str):
        self.o: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.o: B = None
        self.z: A = None
        self.txt = txt

d = A("d")
f = B("f")
h = B("h")

d.o = f
f.o = h
f.z = d
h.o = f
h.z = d
assert f.o.z.o.txt == "
class A:
    def __init__(self, txt: str):
        self.o: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.o: B = None
        self.z: A = None
        self.txt = txt

d = A("d")
f = B("f")
h = B("h")

d.o = f
f.o = h
f.z = d
h.o = f
h.z = d
assert f.o.z.o.txt == "f"