You're creating a whole bunch of values that all share the same pointsList
.
It's not that their instance variables are being reset, it's that all of those variables are names for the exact same value, so when you change that value, they all see it.
In this code:
self.pointsList = pointsList
#create 10 new organisms
for i in range(10):
org = Value(pointsList)
print org.getFitness()
self.values.append(org)
shuffle(pointsList)
… every Value(pointsList)
constructs a new Value
object with the same pointsList
. Inside Value.__init__
, it doesn't copy that list, it just does this:
self.pointsList = pointsList
So, they all end up just referring to the same list.
You probably want to do one of three things:
- Copy the list inside
Value.__init__
(e.g.,self.pointsList = pointsList[:]
). - Pass a copy to
Value.__init__
(e.g.,org = Value(pointsList[:])
). - Create a new list each time through the loop instead of reusing the same one over and over.
I think the third one of these is cleanest in your use case.
I'd write a shuffled
function that returns a new shuffled copy:
def shuffled(seq):
retval = list(seq)
shuffle(retval)
return retval
… so you can do this:
self.pointsList = pointsList
#create 10 new organisms
for i in range(10):
pointsList = shuffled(pointsList)
org = Value(pointsList)
print org.getFitness()
self.values.append(org)