[Tutor] Object problem in python 1.5.2
Ibraheem Umaru-Mohammed
iumarumo@eidosnet.co.uk
Thu, 29 Aug 2002 10:52:39 +0100
["Guillermo Fernandez"="Guillermo"]
.
.
.
Guillermo >>
Guillermo >> The code is:
Guillermo >> In a Class:
Guillermo >>
Guillermo >> def _evaluate(self, mylist):
Guillermo >> """Evaluate the program stored in the list
Guillermo >> IN : The list
Guillermo >> OUT:"""
Guillermo >>
Guillermo >> def iffood():
Guillermo >> # Is there food in front of us?
Guillermo >> line 400 -> length=int(math.sqrt(len(self._temp_map)))
Guillermo >> if self._direction == 0:
Guillermo >> return self._temp_map[self._x_pos+length*
Guillermo >> int(math.fmod((self._y_pos-1),
Guillermo >> length))]
Guillermo >> elif self._direction == 1:
Guillermo >> return self._temp_map[int(math.fmod((self._x_pos+1),
Guillermo >> length))+
Guillermo >> length*self._y_pos]
Guillermo >> elif self._direction == 2:
Guillermo >> return self._temp_map[self._x_pos+length*
Guillermo >> int(math.fmod((self._y_pos+1),
Guillermo >> length))]
Guillermo >> elif self._direction == 3:
Guillermo >> return self._temp_map[int(math.fmod((self._x_pos-1),
Guillermo >> length))+
Guillermo >> length*self._y_pos]
Guillermo >> else:
Guillermo >> print "Error"
Guillermo >>
Guillermo >> executed=0
Guillermo >> length=int(math.sqrt(len(self._temp_map)))
Guillermo >> result=mylist[0]
Guillermo >> if result == "iffood":
Guillermo >> ETC, ETC, ETC...
Guillermo >>
Guillermo >> Have the object interface changed in such a manner that self is not
Guillermo >> recognized anymore?
Guillermo >>
Guillermo >> Of course, the original 2.2 code runs perfectly fine.
Guillermo >>
Guillermo >> Thanks!
Guillermo >>
Guillermo >> Guille
I believe the issue here is one of "nested scopes". Before Python2.2,
names were resolved using the LGB princible (Local, Global, Built-in
namespaces). Since nested scopes have been introduced, unbound names can
be resolved from the enclosing function space. So in the example above,
the method "iffood" is defined within "_evaluate", therefore any
unresolved names in "iffood" are searched for in the "_evaluate"
method's scope. In this case, "self" and "mylist" are resolved in the
enclosing "_evaluate" namespace.
Kindest regards,
--ibs.
--
Ibraheem Umaru-Mohammed
"ibz"
umarumohammed (at) btinternet (dot) com