[英]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.