Python interpreter error, x takes no arguments (1 given)

121,450

Solution 1

Python implicitly passes the object to method calls, but you need to explicitly declare the parameter for it. This is customarily named self:

def updateVelocity(self):

Solution 2

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.

Solution 3

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.

Share:
121,450
Linus
Author by

Linus

Updated on July 08, 2022

Comments

  • Linus
    Linus almost 2 years

    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

  • shaffooo
    shaffooo about 8 years
    I just started learning python. At least for now I think that is ugly.
  • wiz_lee
    wiz_lee almost 8 years
    @fred I was curious did you learn this by exploring along the way while working on an IronPython project or did you have a way of debugging this type of error? Either way I would love to learn =D
  • Drew Delano
    Drew Delano almost 8 years
    @wiz-_-lee: Honestly, I don't remember where exactly I learned it. I think it was from a Python tutorial and then experience. I've never used IronPython or Jython, only CPython.
  • Sagar balai
    Sagar balai almost 7 years
    If python internally handles passing object reference from caller then why it is not handled in called class behaviours. It is very confusing to get such error message and unable to predict cause.
  • Phil
    Phil over 6 years
    This is one of those nasty "gotchas"
  • Apostolos
    Apostolos over 6 years
    'self' produces exactly the same error (for me at least).
  • Admin
    Admin over 5 years
    @Sagarbalai @shaffooo it is not confusing. It is confusing if you don't have experience. That is what learning is for: understanding things you would not otherwise know. self is not a keyword, it's a convention. You can call the first argument whatever you want. Doing what you suggest would need making self a keyword like this in JavaScript, and restructuring Python's scope system. And that would be even more confusing for most cases. Python is dynamic, and that feature goes with its philosophy and structure.
  • Admin
    Admin over 5 years
    What happens, for example, if you are passing class instances as an argument to the class method? There would be a conflict of scopes; and one would shadow the other. It's simply an unncessary complication. Bound methods are passed an instance, unbound are not. Remember: explicit is better than implicit (The Zen of Python). Besides, that would contradict Python's principle of that everything is an object (and how objects are passed by assignment to variables)
  • Drew Delano
    Drew Delano over 5 years
    This is not an answer to the question. move is not a method of a class, so self would not make any sense. move is being given as a callback function. When the screen is clicked, it will call that function with x and y values. You may not use them, but you must accept them.
  • Apostolos
    Apostolos over 5 years
    Right, this applies to a function of the main module, not a class. I gave a general solution to the so common and puzzling Python error in question.