-2

Its probably something simple, but i cant see why boundary_tests is mutating here.

# initalise boundary tests used in other functions

def initialise_boundary_tests(self):

    self.boundary_tests = \
        [self.is_lowest(),
         self.is_highest(),
         self.is_leftmost(),
         self.is_rightmost()]

    self.b0 = self.boundary_tests[0]
    self.b1 = self.boundary_tests[1]
    self.b2 = self.boundary_tests[2]
    self.b3 = self.boundary_tests[3]

    self.set_inverse_bounds()

    if self.debugging:
        print("boundary test: " + str(self.boundary_tests))
        print("inverse boundary test: " + str(self.inverse_boundary_tests))

#initialise inverse results as results
def set_inverse_bounds(self):
    if self.debugging:
        print("setting inverse boundary tests")
        print("boundary test: " + str(self.boundary_tests))
    # self.inverse_boundary_tests = self.boundary_tests
    temp = self.boundary_tests
    self.inverse_boundary_tests = temp
    print("boundary test: " + str(self.boundary_tests))
    print("inverse boundary test: " + str(self.inverse_boundary_tests))
    for index in range(len(self.inverse_boundary_tests)):
        print("index" + str(index))
        if self.inverse_boundary_tests[index] != 0:
            print("before change: "+str(self.inverse_boundary_tests[index]))
            self.inverse_boundary_tests[index] = self.inverse_boundary_tests[index] * -1
            print(self.inverse_boundary_tests[index])
    self.i0 = self.inverse_boundary_tests[0]
    self.i1 = self.inverse_boundary_tests[1]
    self.i2 = self.inverse_boundary_tests[2]
    self.i3 = self.inverse_boundary_tests[3]

Output:

setting inverse boundary tests
boundary test: [1, -1, 0, 1]
boundary test: [1, -1, 0, 1]
inverse boundary test: [1, -1, 0, 1]
index0
before change: 1
-1
index1
before change: -1
1
index2
index3
before change: 1
-1
boundary test: [-1, 1, 0, -1]
inverse boundary test: [-1, 1, 0, -1]
jsky
  • 2,225
  • 5
  • 38
  • 54

1 Answers1

1

In set_inverse_bounds(), self.boundary_tests, temp, and self.inverse_boundary_tests are all referring to the same list. So when you modify the values in self.inverse_boundary_tests, you're modifying the same list that self.boundary_tests is pointing at. I'm not sure why you have temp at all, since you aren't using it anywhere else; unless you thought it would solve this issue.

To make a copy, use this:

self.inverse_boundary_tests = self.boundary_tests[:]

instead of this:

temp = self.boundary_tests
self.inverse_boundary_tests = temp
Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • yeh i didnt realise i needed to create a "new" list in python. but i see i can do it like this now: http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python – jsky Aug 30 '15 at 10:46