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

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

n = A("n")
a = B("a")
h = A("h")

n.z = a
a.p = n
h.z = a
assert h.z.p.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: B = None
        self.txt = txt

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

n = A("n")
a = B("a")
h = A("h")

n.z = a
a.p = n
h.z = a
assert h.z.p.z.txt == "a"