[Tutor] Object problem in python 1.5.2

Guillermo Fernandez guillermo.fernandez@epfl.ch
Thu, 29 Aug 2002 17:01:58 +0930


Hi!

I'm trying to port a 2.2 python program to the 1.5.2 version.

For those who wander why I'm doing such a weird thing:
I've written in my computer (with python 2.2) a program that I would
need to execute now in my university (with python 1.5.2).

I encounter the following error executing my 2.2 program:

Traceback (innermost last):
  File "Genetic.py", line 130, in ?
    if problem.maximize() != 0:
  File "GenAlg.py", line 156, in maximize
    return self.solve()
  File "GenAlg.py", line 113, in solve
    self.__initialize()
  File "GenAlg.py", line 219, in __initialize
    self.m_bestEver.evaluate()
  File "GASantaFe.py", line 508, in evaluate
    program=program-self._evaluate(self.m_genome)
  File "GASantaFe.py", line 435, in _evaluate
    executed=executed+self._evaluate(mylist[1])
  File "GASantaFe.py", line 427, in _evaluate
    if iffood():
  File "GASantaFe.py", line 400, in iffood
    length=int(math.sqrt(len(self._temp_map)))
NameError: self

The code is:
In a Class:

    def _evaluate(self, mylist):
        """Evaluate the program stored in the list
        IN : The list
        OUT:"""

        def iffood():
            # Is there food in front of us?
line 400 -> length=int(math.sqrt(len(self._temp_map)))
            if self._direction == 0:
                return self._temp_map[self._x_pos+length*
                                      int(math.fmod((self._y_pos-1),
length))]
            elif self._direction == 1:
                return self._temp_map[int(math.fmod((self._x_pos+1),
length))+
                                      length*self._y_pos]
            elif self._direction == 2:
                return self._temp_map[self._x_pos+length*
                                      int(math.fmod((self._y_pos+1),
length))]
            elif self._direction == 3:
                return self._temp_map[int(math.fmod((self._x_pos-1),
length))+
                                      length*self._y_pos]
            else:
                print "Error"

        executed=0
        length=int(math.sqrt(len(self._temp_map)))
        result=mylist[0]
        if result == "iffood":
      ETC, ETC, ETC...

Have the object interface changed in such a manner that self is not
recognized anymore?

Of course, the original 2.2 code runs perfectly fine.

Thanks!

Guille