0

So I've been working on this really bad python code I wrote a while ago and as an easy way to optimize it, I tried to use PyPy instead of python 2.7 but while trying to run it, PyPy gives me a list index out of range error whereas python runs the code just fine.

The offending bit of code is attached below -

for front_no in xrange(1, self.no_of_fronts + 1):
        temp = []
        for member in self.population_members:
            if member.front == front_no:
                temp.append(member)
        if(len(temp) == 1):
            temp[0].crowding_distance = float('inf')
        else:
            for member in temp:
                member.crowding_distance = 0
            for y in xrange(0, p.number_of_objectives):
                temp.sort(key=lambda x: x.objective_function_values[y])
                temp[0].crowding_distance = float("inf")
                temp[len(temp) - 1].crowding_distance = float("inf")
                if temp[0].gene == temp[-1].gene:
                    for i in xrange(1, len(temp) - 1):
                        temp[i].crowding_distance = 0

temp[0].crowding_distance = float("inf") is what's causing the problem specifically, but there's no reason for temp to be empty at that point.

  • 1
    _"but there's no reason for temp to be empty at that point"_ What if `self.population_members` is empty or none of them have a matching `front_no`? – Aran-Fey Jun 05 '17 at 15:12
  • That should never happen because of the way the rest of the code works. Should I paste the rest of it? I didn't think people would care to read a couple of hundred lines of code. Again, the code runs fine in python, just not under pypy, so I'm a little lost – Karmanya Aggarwal Jun 05 '17 at 15:22
  • 1
    But how do you know that __this__ is where PyPy behaves differently from CPython? Maybe in PyPy `self.population_members` _is_ empty. It's probably best to debug it as you usually would - regardless of the fact that it works in CPython. Add some `print` statements to see what's going on. – Aran-Fey Jun 05 '17 at 15:33

0 Answers0