Python 解释器错误,x 不带参数(给出 1 个)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4445405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python interpreter error, x takes no arguments (1 given)
提问by Linus
I'm writing a small piece of python as a homework assignment, and I'm not getting it to run! I don't have that much Python-experience, but I know quite a lot of Java. I'm trying to implement a Particle Swarm Optimization algorithm, and here's what I have:
我正在写一小段 python 作为家庭作业,但我没有让它运行!我没有那么多 Python 经验,但我知道很多 Java。我正在尝试实现粒子群优化算法,这是我所拥有的:
class Particle:
def __init__(self,domain,ID):
self.ID = ID
self.gbest = None
self.velocity = []
self.current = []
self.pbest = []
for x in range(len(domain)):
self.current.append(random.randint(domain[x][0],domain[x][1]))
self.velocity.append(random.randint(domain[x][0],domain[x][1]))
self.pbestx = self.current
def updateVelocity():
for x in range(0,len(self.velocity)):
self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 * random.random()*(self.gbest[x]-self.current[x])
def updatePosition():
for x in range(0,len(self.current)):
self.current[x] = self.current[x] + self.velocity[x]
def updatePbest():
if costf(self.current) < costf(self.best):
self.best = self.current
def psoOptimize(domain,costf,noOfParticles=20, noOfRuns=30):
particles = []
for i in range(noOfParticles):
particle = Particle(domain,i)
particles.append(particle)
for i in range(noOfRuns):
Globalgbest = []
cost = 9999999999999999999
for i in particles:
if costf(i.pbest) < cost:
cost = costf(i.pbest)
Globalgbest = i.pbest
for particle in particles:
particle.updateVelocity()
particle.updatePosition()
particle.updatePbest(costf)
particle.gbest = Globalgbest
return determineGbest(particles,costf)
Now, I see no reason why this shouldn't work. However, when I run it, I get this error:
现在,我看不出为什么这不可行。但是,当我运行它时,出现此错误:
"TypeError: updateVelocity() takes no arguments (1 given)"
“类型错误:updateVelocity() 不接受任何参数(给定 1 个)”
I don't understand! I'm not giving it any arguments!
我不明白!我不给它任何论据!
Thanks for the help,
谢谢您的帮助,
Linus
莱纳斯
采纳答案by Fred Larson
Python implicitly passes the object to method calls, but you need to explicitly declare the parameter for it. This is customarily named self:
Python 将对象隐式传递给方法调用,但您需要为它显式声明参数。这通常被命名为self:
def updateVelocity(self):
回答by bgporter
Your updateVelocity()method is missing the explicit selfparameter in its definition.
您的updateVelocity()方法self在其定义中缺少显式参数。
Should be something like this:
应该是这样的:
def updateVelocity(self):
for x in range(0,len(self.velocity)):
self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 \
* random.random()*(self.gbest[x]-self.current[x])
Your other methods (except for __init__) have the same problem.
您的其他方法(除了__init__)也有同样的问题。
回答by miku
Make sure, that allof your class methods (updateVelocity, updatePosition, ...) take at least one positional argument, which is canonically named selfand refers to the current instance of the class.
确保您的所有类方法(updateVelocity, updatePosition, ...)至少采用一个位置参数,该参数被规范命名self并引用该类的当前实例。
When you call particle.updateVelocity(), the called method implicitly gets an argument: the instance, here particleas first parameter.
当您调用 时particle.updateVelocity(),被调用的方法隐式地获得一个参数:实例,这里particle作为第一个参数。
回答by Apostolos
I have been puzzled a lot with this problem, since I am relively new in Python. I cannot apply the solution to the code given by the questioned, since it's not self executable. So I bring a very simple code:
我对这个问题很困惑,因为我是 Python 新手。我无法将解决方案应用于被质疑者给出的代码,因为它不是自我可执行的。所以我带来了一个非常简单的代码:
from turtle import *
ts = Screen(); tu = Turtle()
def move(x,y):
print "move()"
tu.goto(100,100)
ts.listen();
ts.onclick(move)
done()
As you can see, the solution consists in using two (dummy) arguments, even if they are not used either by the function itself or in calling it! It sounds crazy, but I believe there must be a reason for it (hidden from the novice!).
如您所见,解决方案包括使用两个(虚拟)参数,即使它们没有被函数本身使用或调用它!这听起来很疯狂,但我相信一定有原因(对新手隐藏!)。
I have tried a lot of other ways ('self' included). It's the only one that works (for me, at least).
我尝试了很多其他方式(包括“自我”)。这是唯一有效的方法(至少对我而言)。

