[python] 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! 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:

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)"

I don't understand! I'm not giving it any arguments!

Thanks for the help,

Linus

This question is related to python object methods arguments

The answer is


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:

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).


Your updateVelocity() method is missing the explicit self parameter in its definition.

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.


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.

When you call particle.updateVelocity(), the called method implicitly gets an argument: the instance, here particle as first parameter.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor

Examples related to methods

String method cannot be found in a main class method Calling another method java GUI ReactJS - Call One Component Method From Another Component multiple conditions for JavaScript .includes() method java, get set methods includes() not working in all browsers Python safe method to get value of nested dictionary Calling one method from another within same class in Python TypeError: method() takes 1 positional argument but 2 were given Android ListView with onClick items

Examples related to arguments

docker build with --build-arg with multiple arguments ARG or ENV, which one to use in this case? How to have multiple conditions for one if statement in python Gradle task - pass arguments to Java application Angularjs - Pass argument to directive TypeError: method() takes 1 positional argument but 2 were given Best way to check function arguments? "Actual or formal argument lists differs in length" Python: Passing variables between functions Print multiple arguments in Python