Python interpreter error, x does not accept arguments (1 given)

I am writing a small piece of python as a homework, and I do not make it work! I don't have much Python experience, but I know quite a lot of Java. I am trying to implement a swarm of particle optimization algorithm, and here is 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 should not work. However, when I run it, I get this error:

"TypeError: updateVelocity () does not accept arguments (1 given)"

I do not understand! I do not give any arguments!

Thanks for the help,

Linus

+58
python object methods arguments
Dec 14 '10 at 23:40
source share
5 answers

Python implicitly passes an object to method calls, but you need to explicitly declare a parameter for it. This is usually called self :

 def updateVelocity(self): 
+119
Dec 14 '10 at 23:42
source share

Make sure that all your class methods ( updateVelocity , updatePosition , ...) accept at least one positional argument, which is canonically called self and refers to the current instance of the class.

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

+8
Dec 14 '10 at 23:45
source share

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

There 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]) 

Other methods (except __init__ ) have the same problem.

+6
Dec 14 '10 at 23:45
source share

I was very puzzled by this problem since I recently appeared in Python. I cannot apply the solution to the code asked by the respondents, since it is not executable on its own. Therefore, I give 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 is to use two (fictitious) arguments , even if they are not used by the function itself or when it is called! It sounds crazy, but I think there must be a reason for this (hidden from the newbie!).

I tried many other ways (including "myself"). This is the only one that works (for me, at least).

0
Jan 21 '18 at 18:44
source share

Dog () does not accept arguments

Class Dog ():

 def _init_(self, name, age): self.name = name self.age = age def sit(self): print(self.name.title() + " is now sitting.") def roll_over(self): print(self.name.title() + "is now rolled over.") 

my_dog = Dog ("volume", 6)

0
May 03 '19 at 8:17
source share



All Articles