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

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

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

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

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

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

a.o = h
h.q = a
h.p = a
m.o = h
assert a.o.p.o.txt == "h"