When you have two classes that need to have attributes that refer to each other
# DOESN'T WORK
class A:
    b = B()
class B:
    a = A()
# -> ERROR: B is not defined
The standard answers say to use the fact that python is dynamic, ie.
class A:
    pass
class B:
    a = A()
A.b = B()
Which technically solves the problem. However, when there are three or more co-dependent classes, or when the classes are more than a few lines long, this approach results in spaghetti code that is extremely difficult to navigate. For example, I find myself writing code like:
class A:
    <50 lines>
    # a = B() but its set later
    <200 more lines>
class B:
    <50 lines>
    a = A()
    <100 lines>
A.b = B()  # to allow for circular referencing
This ends up violating DRY (since I write code in two places) and/or moves related code to opposite ends of my module, since I can't put A.b = B() in the class it's relevant to.
Is there a better method to allow circularly dependent class properties in python, that doesn't involve scattering related code to often-distant parts of the module?
 
    