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

Re: [pygame] Name self not defined



Marius Gedminas wrote:
On Tue, Jan 27, 2009 at 07:24:13PM -0800, James Paige wrote:
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.

The idiomatic way to express that is 'if speedv is None:'.
See http://www.python.org/dev/peps/pep-0008/

Marius Gedminas
Don't know how `idiomatic` or `pythonic` it is but I see a lot of people use `or` expressions:

class Car(Basicsprite): #car class
def move(self, speedv=None): self.rect = self.rect.move(speedv or (0,0))

no `speedv`, in (x,y) tuple format, is ever going to bool as False
so if it `is None` it will use `self.speedv` of (0,0)