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.x: A = None
        self.txt = txt

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

k.p = f
f.z = k
f.x = k
g.z = k
g.x = k
assert f.x.p.x.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.x: A = None
        self.txt = txt

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

k.p = f
f.z = k
f.x = k
g.z = k
g.x = k
assert f.x.p.x.txt == "k"