title: How to link parent and children to each other?
body: <p>Having two simple classes; one with only <code>parent</code> attribute, and one with both <code>parent</code> and <code>children</code> attributes.
  This means that the one with both <code>parent</code> and <code>children</code> inherits from the one with only <code>parent</code>.</p>
  
  <p>Here's the class with only <code>parent</code> attribute.
  Let's call it <code>Child</code> since it can only be a child, not a parent.
  I'll use a method <code>set_parent()</code> to make it more clear, but I would use a setter in my actual code.</p>
  
  <pre><code>class Child(object):
  
      def __init__(self, parent=None):
          self.__parent = None
          self.set_parent(parent)
  
      def set_parent(self, parent):
          # Remove self from old parent's children
          if self.__parent:
              self.__parent.remove_child(self)
          # Set new parent
          self.__parent = parent
          # Add self to new parent's children
          if self.__parent:
              self.__parent.add_child(self)
  </code></pre>
  
  <p>The code makes perfect sense and seems to work just fine.
  This is, if the <code>Parent</code> class looks as simple as this:</p>
  
  <pre><code>class Parent(Child):
  
      def __init__(self, parent=None):
          super(Parent, self).__init__(parent)
          self.__children = []
  
      def add_child(self, child):
          if child not in self.__children:
              self.__children.append(child)
  
      def remove_child(self, child):
          if child in self.__children:
              self.__children.remove(child)
  </code></pre>
  
  <p>However, I want to be able to call <code>my_parent.add_child(my_child)</code> and have <code>my_child</code>'s parent attribute set to <code>my_parent</code> while removing <code>my_child</code> from it's old parent's children.</p>
  
  <p>I can't seem to figure out how to actually <em>design</em> the code, everything I try will turn into an infinite loop between <code>set_parent()</code> and <code>add_child()</code> or <code>remove_child()</code>.</p>
  
  <p>I know this site is not meant for other people to write code for me, but could someone at least give some hints?
  My brain just can't handle this problem, I've been thinking for 30 minutes straight and haven't gotten anything done.
  Help appreciated!</p>