English 中文(简体)
粉碎:为什么一种方法与附加参数不同?
原标题:Python: Why Does a Method Behave Differently with an Added Parameter?

我采用一种Pyega 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

我作了修改,增加了一个参数:<代码>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

在我这样说之前:

def update(self):
        self.walk()

现在我做的是:

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

为什么不开展这项工作?

最佳回答

你没有解释它是如何“经纪”的,但主要区别是:

speed_x = -speed_x

你在第二版中仅改动了 当地<>>/em>变量(标记是当地变量!)speed_x,以便改变价值不会持续下去。

第1版

self.move = -self.move 

确实改变了<条码>自封/条码>(具体说是条码之一)以及未来方法中的“老板”改动,把此处的物体称作<条码>。

仅是光名称(如speed_x)和合格名称(.move)之间的许多关键差别之一,而且,我怀疑,在座各位(如果你不说how,第二版就会辜负你们的期望)。

问题回答

你们不会自食其果。 流动。

如果你想使用你的法典第二版,试图增加这一行文:

    self.move = speed_x

页: 1

如同其他人一样,你不改动<代码>。 页: 1

如果你想把不同的论据传递给你,并作修改,你可以传递<条码>的修改值。 作为回报值:

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

将这一职能称为:

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

<>说明: 回答假定<代码>本身.move 在打电话<代码>walk时,不应总是更新。 如果这一假设是假的,则<代码>本身。 事实上,每当操作<代码>walk<>/code>时,就应当更新。 回答。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...