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

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

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

f.p = g
f.y = g
e.x = f
e.z = f
g.x = f
g.z = f
assert f.p.z.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.y: B = None
        self.txt = txt

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

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

f.p = g
f.y = g
e.x = f
e.z = f
g.x = f
g.z = f
assert f.p.z.p.txt == "g"