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

Re: [pygame] Name self not defined



On Tue, Jan 27, 2009 at 07:18:20PM -0800, Yanom Mobis 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??

You can't use "self" in the "def" line. Here is how I usually accomplish 
what you are trying to do:

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


But maybe somebody else can suggest a nicer way to do that.

---
James Paige