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

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

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

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

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

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

d.z = f
g.s = f
g.z = f
f.z = d
assert g.s.z.z.txt == "f"