简体   繁体   English

Python:为什么带有附加参数的方法的行为有所不同?

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

I have a method in a Pygame Sprite subclass, defined as such: 我在Pygame Sprite子类中有一个方法,其定义如下:

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. 我修改了它,添加了一个参数speed_x ,现在程序已损坏。

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? 为什么不起作用?

You don't explain how it's "broken", but the main difference is that 您无需说明它是如何“断开”的,但主要区别在于

speed_x = -speed_x

which you have in your second version, is only changing the local variable (arguments are local variables!) speed_x , so that changed value does not persist. 在第二个版本中,它仅更改了局部变量(参数是局部变量!) speed_x ,因此更改后的值不会持续存在。

In the first version, 在第一个版本中

self.move = -self.move 

does alter self (specifically one of its attriubtes) and the alteration "persists" in future method calls on the object which is here accessed as self . 确实会更改self (特别是其属性之一),并且在将来的方法中更改“ persists”将在此调用作为self访问的对象。

Just one of the many key differences between bare names (like speed_x ) and qualified names (line self.move ), and, I suspect, what's biting you here (hard as you may make it to guess by not saying how the second version is failing your expectations). 只是裸露的名字(例如之间的许多主要区别之一speed_x )和合格的名称(线self.move ),并且,我怀疑,怎么在这里咬你(努力,你可以让它通过不是说第二个版本是如何猜测达不到您的期望)。

您无需将偏移量存储回self.move中。

If you want to use the second version of your code, try adding this line: 如果要使用第二版代码,请尝试添加以下行:

    self.move = speed_x

At the bottom of your function. 在功能的底部。

As mentioned by others, you are not changing the value of self.move in your new code. 正如其他人提到的那样,您不会在新代码中更改self.move的值。 I assume the reason you modified this function was so you could reuse this function for values other than self.move . 我认为您修改此函数的原因是为了使您可以针对self.move以外的其他值重用此函数。

If you want to be able to pass different arguments into your function and modify them as well, you could pass the modified value of speed_x back as a return value: 如果您希望能够将不同的参数传递给函数并进行修改,则可以将speed_x的修改后的值作为返回值speed_x回:

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
    return speed_x

And call the function like this as: 并按如下所示调用函数:

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

Note: This answer assumes that self.move should not always be updated when calling walk . 注意:此答案假设在调用walk时,不应始终更新self.move If this assumption is false and self.move should in fact be updated every time walk is run, then you should instead use Xavier Ho's answer. 如果这个假设是错误的,并且实际上self.move应该在每次walk更新,那么您应该改用Xavier Ho的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不同的格式方法在 Python 中表现不同? - Why different format method behave differently in Python? 为什么 Python 3 for loop output 和行为不同? - Why does Python 3 for loop output and behave differently? 为什么我的python类方法本身的行为与我运行模块时的行为不同? - Why does my python class method behave differently by itself than when I run the module? 为什么hasattr在使用@property方法的类和实例上表现不同? - Why does hasattr behave differently on classes and instances with @property method? 为什么这个argparse代码在Python 2和3之间表现不同? - Why does this argparse code behave differently between Python 2 and 3? Python 2-为什么“ with”在嵌入式C代码中表现不同? - python 2 - why does 'with' behave differently in embedded c code? 为什么Python列表的行为会根据声明的不同而有所不同? - Why does Python list of lists behave differently depending on their declaration? 为什么Python **运算符在数组和标量上的行为不同 - Why does the Python ** operator behave differently on arrays and scalars 为什么 Python 对相同的函数(例如“sum”或“and”)表现不同? - Why does Python behave differently for the same function such as 'sum' or 'and'? 为什么RestrictedPython在与Python 3.6一起使用时表现不同? - Why does RestrictedPython behave differently when used with Python 3.6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM