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

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

d = A("d")
m = B("m")
b = B("b")

d.y = m
m.y = b
b.y = m
assert m.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

d = A("d")
m = B("m")
b = B("b")

d.y = m
m.y = b
b.y = m
assert m.y.y.y.txt == "b"