[英]Python interpreter error, x takes no arguments (1 given)
I'm writing a small piece of python as a homework assignment, and I'm not getting it to run!我正在写一小段 python 作为家庭作业,但我没有让它运行! I don't have that much Python-experience, but I know quite a lot of Java.
我没有那么多 Python 经验,但我知道很多 Java。 I'm trying to implement a Particle Swarm Optimization algorithm, and here's what I have:
我正在尝试实现粒子群优化算法,这是我所拥有的:
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莱纳斯
Python implicitly passes the object to method calls, but you need to explicitly declare the parameter for it. Python 将对象隐式传递给方法调用,但您需要为它显式声明参数。 This is customarily named
self
:这通常被命名为
self
:
def updateVelocity(self):
Make sure, that all of your class methods ( updateVelocity
, updatePosition
, ...) take at least one positional argument, which is canonically named self
and 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 particle
as first parameter.当您调用
particle.updateVelocity()
,被调用的方法会隐式地获取一个参数:实例,这里是particle
作为第一个参数。
Your updateVelocity()
method is missing the explicit self
parameter 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__
)也有同样的问题。
I have been puzzled a lot with this problem, since I am relively new in Python.我对这个问题很困惑,因为我是 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:
所以我带来了一个非常简单的代码:
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).
这是唯一有效的方法(至少对我而言)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.