title: Python TreeStructure (parent and children) class, children gets appended to it's own children?
body: <p>I'm coding a <code>TreeStructure</code> (TS) class, which lets me create parent and child objects, which are linked to each other. Every TS -object has <code>m_parent</code> attribute, which is controlled with <code>parent</code> property, and also they have <code>children</code> list, which holds in all the children of this parent. Whenever I add a child to it's parent's <code>children</code> list, it gets added to it's own <code>children</code> list too? Here's what I have:</p>
  
  <pre><code>ROOT = "__ROOT__"
  
  class TreeStructure:
  
      def __init__(self, parent=ROOT, children=[]):
          self.children = children
          self.parent = parent
  
      @property
      def parent(self):
          '''Returns m_parent'''
          if hasattr(self, "m_parent"):
              return self.m_parent
          else:
              return None
  
      @parent.setter
      def parent(self, parent=ROOT):
          '''Sets m_parent'''
          if type(parent) == type(self):
              if self.parent:
                  del self.parent
              self.m_parent = parent
              self.m_parent.children.append(self)
          elif parent == ROOT:
              if self.parent:
                  del self.parent
              self.m_parent = ROOT
          else:
              raise TypeError("Parent's type %s did not match objects type %s"
                              %(type(parent), type(self)))
  
      @parent.deleter
      def parent(self):
          '''Deletes m_parent'''
          if self.parent:
              if self.parent != ROOT:
                  self.m_parent.children.remove(self)
              del self.m_parent
  </code></pre>
  
  <p>And now by creating two simple objects, it should work. However, it doesn't.</p>
  
  <pre><code>a = TreeStructure()
  b = TreeStructure(a)
  </code></pre>
  
  <p>The problem appears at line 25, <code>self.m_parent.children.append(self)</code>. If I add print's to both sides of that line, I see that both <code>print(self.m_parent.children)</code> and <code>print(self.children)</code> print an empty list <code>[]</code> BEFORE the append line. Now if I add the prints AFTER the append line, both prints will say <code>[&lt;__main__.TreeStructure object at 0x...&gt;]</code>, which should only happen for the parent, not the child?</p>