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

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

a = A("a")
d = B("d")
e = A("e")

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

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

a = A("a")
d = B("d")
e = A("e")

a.p = d
d.z = a
e.p = d
assert a.p.z.p.txt == "d"