[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Name self not defined





On Wed, Jan 28, 2009 at 2:18 PM, Yanom Mobis <yanom@xxxxxxxxxxxxxx> wrote:
this is the code in question:

class Car(Basicsprite): #car class
   def __init__(self, speedarg=(0,0)):
       self.speed = speedarg
       Basicsprite.__init__(self)
   def move(self, speedv=self.speed):
       self.rect = self.rect.move(speedv)


however, when I try to import that class from python:



>>> from basicsprite import Car
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "basicsprite.py", line 14, in <module>
   class Car(Basicsprite): #car class
 File "basicsprite.py", line 18, in Car
   def move(self, speedv=self.speed):
NameError: name 'self' is not defined



how can self not be defined??



This line contains your error.
 
def move(self, speedv=self.speed):

At the time the default arguments are evaluated (at the time of function definition), self - your instance - is not defined. 

Try something along these lines instead:

def move(self, speedv=None):
  if speedv is None:
    speedv = self.speed