title: Loop in functions calling each other
body: <p>I have a class that represents a person in a family (FamilyMember).</p>
  
  <p>I implemented the function <code>setParent</code> that receives a FamilyMember Object and adds it to the list of <code>parent</code> of the current Object.
  Exactly the same is done in the <code>setChild</code> function.</p>
  
  <p><strong>The problem</strong>: as one function calls the other (If we set the parent of our current Object we should as well set the current Object as the parent's child), they enter in a loop. => I managed to stop it checking if the object was already in the list. But this means I'm alway doing an unnecessary call to the function.</p>
  
  <pre><code>    def setParent(self, parent):
          if not isinstance(parent, FamilyMember):
              raise TypeError("Parent must be set to a FamilyMember")
          if parent in self.parents:
              print("parent already in the list")
              return
          self.parents.append(parent) 
          parent.setChild(self)
  
  
      def setChild(self, child):
  
          if not isinstance(child, FamilyMember):
              raise TypeError("child must be set to a FamilyMember")
          if child in self.children:
              print("child already in the list")
              return
          self.children.append(child) 
          child.setParent(self)
  </code></pre>
  
  <p><strong>Posible solution that I dislike</strong>: I could do 2 more functions that do the same but don't call the other function. But this means repeating code, and having 4 different functions, which is not a good practice. 
  ex:</p>
  
  <pre><code>    def setParent_no_loop(self, parent):
          if not isinstance(parent, FamilyMember):
              raise TypeError("Parent must be set to a FamilyMember")
          if parent in self.parents:
              print("parent already in the list")
              return
          self.parents.append(parent) 
  
      def setChild(self, child):
          if not isinstance(child, FamilyMember):
              raise TypeError("child must be set to a FamilyMember")
          if child in self.children:
              print("child already in the list")
              return
          self.children.append(child) 
          self.setParent_no_loop(self)
  </code></pre>
  
  <p><strong>Posible solution that I dislike</strong>: I rewrite the code from the other function into both functions. Which, again is rewriting code, and its not really elegant:</p>
  
  <pre><code>    def setParent_no_loop(self, parent):
          if not isinstance(parent, FamilyMember):
              raise TypeError("Parent must be set to a FamilyMember")
          if parent in self.parents:
              print("parent already in the list")
              return
          self.parents.append(parent)
          parent.children.append(self) 
  </code></pre>
  
  <p><strong>The question</strong> is, can you find an elegant way to solve this problem?</p>