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

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

a = A("a")
b = B("b")
g = A("g")

a.y = b
b.w = a
g.y = b
assert b.w.y.w.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

a = A("a")
b = B("b")
g = A("g")

a.y = b
b.w = a
g.y = b
assert b.w.y.w.txt == "a"