Python: Why Does a Method Behave Differently with an Added Parameter?

Posted by SteveStifler on Stack Overflow See other posts from Stack Overflow or by SteveStifler
Published on 2010-05-26T03:29:50Z Indexed on 2010/05/26 3:31 UTC
Read the original article Hit count: 257

Filed under:
|
|

I have a method in a Pygame Sprite subclass, defined as such:

def walk(self):
    """move across screen"""
    displacement = self.rect.move((self.move, 0))
    if self.rect.left < self.area.left or self.rect.right > self.area.right:
        self.move = -self.move
        displacement = self.rect.move((self.move, 0))
    self.rect = displacement

I modified it, adding a parameter speed_x, and now the program is broken.

def walk(self, speed_x):
    """move across screen"""
    displacement = self.rect.move((speed_x, 0))
    if self.rect.left < self.area.left or self.rect.right > self.area.right:
        speed_x = -speed_x
        displacement = self.rect.move((speed_x, 0))
    self.rect = displacement

Before I called the method like this:

def update(self):
        self.walk()

Now I do:

def update(self):
    self.walk(self.move)

Why doesn't this work?

© Stack Overflow or respective owner

Related posts about python

Related posts about oop