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

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

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

k.w = f
f.w = k
h.w = f
assert k.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.txt = txt

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

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

k.w = f
f.w = k
h.w = f
assert k.w.w.w.txt == "f"