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

Re: [pygame] Name self not defined



oh, thanks.

--- On Tue, 1/27/09, James Paige <Bob@xxxxxxxxxxxxxxxxxxx> wrote:

From: James Paige <Bob@xxxxxxxxxxxxxxxxxxx>
Subject: Re: [pygame] Name self not defined
To: pygame-users@xxxxxxxx
Date: Tuesday, January 27, 2009, 9:24 PM


-----Inline Attachment Follows-----

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