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

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

a = A("a")
d = B("d")
m = B("m")
h = B("h")

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

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

a = A("a")
d = B("d")
m = B("m")
h = B("h")

a.y = m
a.v = h
d.z = a
d.p = a
m.z = a
m.p = a
h.z = a
h.p = a
assert d.z.v.z.v.txt == "h"