简体   繁体   English

Python 解释器错误,x 不带参数(给出 1 个)

[英]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.确保您的所有类方法( updateVelocityupdatePosition等)至少采用一个位置参数,该参数被规范地命名为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.

相关问题 要求输入错误的方法的Python 3.x错误为TypeError:__init __()接受0个位置参数,但给出了1个 - Python 3.x error with methods asking for input error is TypeError: __init__() takes 0 positional arguments but 1 was given Python错误:X()只取1个参数(给定8个) - Python error : X() takes exactly 1 argument (8 given) python函数x恰好接受n个参数(给定n-1) - python function x takes exactly n arguments (n-1 given) 错误正好需要2个参数(给定3个) - Error takes exactly 2 arguments (3 given) “采用 0 个位置参数,但给出了 1 个”错误 - "takes 0 positional arguments but 1 was given" error Python错误TypeError:__init __()恰好接受2个参数(给定1个) - Python error TypeError: __init__() takes exactly 2 arguments (1 given) Python2.7中的错误正好需要2个参数(给定1个) - Error in Python2.7 takes exactly 2 arguments (1 given) Python 单元测试用例错误:恰好需要 2 个 arguments(给定 3 个) - Python Unit Test Case error: takes exactly 2 arguments (3 given) Python错误:__ init __()至少需要3个参数(给定3个) - Python error: __init__() takes at least 3 arguments (3 given) Python 请求 POST 错误:__init__() 需要 2 个参数(给定 1 个) - Python Requests POST error: __init__() takes 2 arguments (1 given)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM