title: Python: Parent and Child responding to child-specific changes in boolean values. Why is this happening?
body: <p>EDIT: Case closed. See the chosen answer below for a solution (thanks rnorris!)</p>
  
  <p>The questions asked are below.</p>
  
  <hr>
  
  <p>I have gotten some insight on my question, and I now believe that the question is more about how to set things equal to each other in Python, but only in values. From my understanding, I am running into problems because I want to first set the children equal to their parent, and then change them as necessary. The problem is that when I say </p>
  
  <pre><code>child = parent
  </code></pre>
  
  <p>It makes them refer to the same space in memory, and thus any changes to child values become changes to parent values. Is there a way to set child equal to parent while still allowing their values to be edited separately?</p>
  
  <p>The original question is below.</p>
  
  <hr>
  
  <p>This is another question about my project with booleans. I am trying to figure out why my project is behaving in a strange way. My apologies for a very long question, I hope you are able to help me!</p>
  
  <p>My project is to model a graph search, and my problem is with the parents and children. How it works is that there are up to four possible actions to be taken on the parent to produce a new child node: flipping 'farmer' from true to false (or vice versa), and then flipping both farmer and any of three other bools, wolf, sheep, and cabbage, but only if they are equal to farmer (ie. if wolf and farmer are both true, then flip both of them to make a new child). In the case of my initial parent node, all four bools are false, so there should be four child nodes made, one for each event.</p>
  
  <pre><code>#boolfliptest
  
  class node:
      def __init__(self):
          self.farmer, self.wolf, self.sheep, self.cabbage = False, False, False, False
          self.parent = None
          self.cost = 0
  
  parent = node()
  fchild = node()
  schild = node()
  wchild = node()
  cchild = node()
  
  fchild = parent
  fchild.farmer = (True, False)[fchild.farmer]
  fchild.parent = parent
  fchild.cost += 1
  
  if(parent.farmer == parent.wolf):
      wchild = parent
      wchild.farmer != wchild.farmer
      wchild.wolf != wchild.wolf
      wchild.parent = parent
      wchild.cost += 1
  
  if(parent.farmer == parent.sheep):
      schild = parent
      schild.farmer != schild.farmer
      schild.sheep != schild.sheep
      schild.parent = parent
      schild.cost += 1
  
  if(parent.farmer == parent.cabbage):
      cchild = parent
      cchild.farmer != cchild.farmer
      cchild.cabbage != wchild.cabbage
      cchild.parent = parent
      cchild.cost += 1
  
  
  print parent.farmer, parent.wolf, parent.sheep, parent.cabbage
  print fchild.farmer, fchild.wolf, fchild.sheep, fchild.cabbage
  print wchild.farmer, wchild.wolf, wchild.sheep, wchild.cabbage
  print schild.farmer, schild.wolf, schild.sheep, schild.cabbage
  print cchild.farmer, cchild.wolf, cchild.sheep, cchild.cabbage
  </code></pre>
  
  <p>This indeed happens, as I am using a print line to print out the four boolean values for the parent and the four children. They should come out as follows:</p>
  
  <pre><code>    False False False False #parent
      True  False False False #farmer flip only
      True  True  False False #farmer and wolf flip
      True  False True  False #farmer and sheep flip
      True  False False True  #farmer and cabbage flip
  </code></pre>
  
  <p>But instead I only get this:</p>
  
  <pre><code> % python boolfliptest.py
      False False False False
      False False False False
      False False False False
      False False False False
      False False False False
  </code></pre>
  
  <p>I thought maybe I was using a poor method to toggle bools, and apparently I was, as the original method </p>
  
  <pre><code>schild.farmer != schild.farmer #taken from above
  </code></pre>
  
  <p>did not work. I replaced just the fchild flip statement with this new way of toggling bools</p>
  
  <pre><code>fchild.farmer = (True, False)[fchild.farmer]
  </code></pre>
  
  <p>And I did get better results, but not what I expected. The result was this:</p>
  
  <pre><code>% python boolfliptest.py
  True False False False
  True False False False
  False False False False
  False False False False
  False False False False
  </code></pre>
  
  <p>The fchild (second on the list) did flip correctly, like I expected, but so too did the parent node, which should have remained all false!</p>
  
  <p>I tried one other thing. I commented out the fchild.farmer bool flip line, and instead used that new method with the wchild bools. ie:</p>
  
  <pre><code>if(parent.farmer == parent.wolf):
      wchild = parent
      wchild.farmer = (True, False)[wchild.farmer]
      wchild.wolf = (True, False)[wchild.wolf]
      wchild.parent = parent
      wchild.cost += 1
  </code></pre>
  
  <p>This result was equally confusing. The wchild flipping worked correctly, but it was copied for both the fchild and the parent, neither of which should have changed. </p>
  
  <pre><code>% python boolfliptest.py
  True True False False
  True True False False
  True True False False
  False False False False
  False False False False
  </code></pre>
  
  <p>Can anyone explain what is happening here? Why are the bools so odd?</p>