title: python circular imports once again (aka what's wrong with this design)
body: <p>Let's consider python (3.x) scripts:</p>
  
  <p>main.py:</p>
  
  <pre><code>from test.team import team
  from test.user import user
  
  if __name__ == '__main__':
      u = user()
      t = team()
      u.setTeam(t)
      t.setLeader(u)
  </code></pre>
  
  <p>test/user.py:</p>
  
  <pre><code>from test.team import team
  
  class user:
      def setTeam(self, t):
          if issubclass(t, team.__class__):
              self.team = t
  </code></pre>
  
  <p>test/team.py:</p>
  
  <pre><code>from test.user import user
  
  class team:
      def setLeader(self, u):
          if issubclass(u, user.__class__):
              self.leader = u
  </code></pre>
  
  <p>Now, of course, i've got circular import and splendid ImportError.</p>
  
  <p>So, not being pythonista, I have three questions. First of all:</p>
  
  <p>i. How can I make this thing work ? </p>
  
  <p>And, knowing that someone will inevitably say "Circular imports always indicate a design problem", the second question comes:</p>
  
  <p>ii. Why is this design bad?</p>
  
  <p>And the finally, third one:</p>
  
  <p>iii. What would be better alternative?</p>
  
  <p>To be precise, type checking as above is only an example, there is also a index layer based on class, which permits ie. find all users being members of one team (user class has many subclasses, so index is doubled, for users in general and for each specific subclass) or all teams having given user as a member</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I hope that more detailed example will clarify what i try to achieve. Files omitted for readibility (but having one 300kb source file scares me somehow, so please assume that every class is in different file)</p>
  
  <pre><code># ENTITY
  
  class Entity:
      _id    = None
      _defs  = {}
      _data  = None
  
      def __init__(self, **kwargs):
          self._id   = uuid.uuid4() # for example. or randint(). or x+1.
          self._data = {}.update(kwargs)
  
      def __settattr__(self, name, value):
          if name in self._defs:
              if issubclass(value.__class__, self._defs[name]):
                  self._data[name] = value
  
                  # more stuff goes here, specially indexing dependencies, so we can 
                  # do Index(some_class, name_of_property, some.object) to find all   
                  # objects of some_class or its children where
                  # given property == some.object
  
              else:
                  raise Exception('Some misleading message')
          else:
              self.__dict__[name] = value    
  
      def __gettattr__(self, name):
          return self._data[name]
  
  # USERS 
  
  class User(Entity):
      _defs  = {'team':Team}
  
  class DPLUser(User):
      _defs  = {'team':DPLTeam}
  
  class PythonUser(DPLUser)
      pass
  
  class PerlUser(DPLUser)
      pass
  
  class FunctionalUser(User):
      _defs  = {'team':FunctionalTeam}
  
  class HaskellUser(FunctionalUser)
      pass
  
  class ErlangUser(FunctionalUser)
      pass
  
  # TEAMS
  
  class Team(Entity):
      _defs  = {'leader':User}
  
  class DPLTeam(Team):
      _defs  = {'leader':DPLUser}
  
  class FunctionalTeam(Team):
      _defs  = {'leader':FunctionalUser}
  </code></pre>
  
  <p>and now some usage:</p>
  
  <pre><code>t1 = FunctionalTeam()
  t2 = DLPTeam()
  t3 = Team()
  
  u1 = HaskellUser()
  u2 = PythonUser()
  
  t1.leader = u1 # ok
  t2.leader = u2 # ok
  t1.leader = u2 # not ok, exception
  t3.leader = u2 # ok
  
  # now , index
  
  print(Index(FunctionalTeam, 'leader', u2)) # -&gt; [t2]
  print(Index(Team, 'leader', u2)) # -&gt; [t2,t3]
  </code></pre>
  
  <p>So, it works great (implementation details ommitted, but there is nothing complicated) besides of this unholy circular import thing. </p>