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

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

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

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

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

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

d.s = g
g.z = d
g.y = m
m.z = d
m.y = g
assert d.s.z.s.txt == "g"