Mark Finch
Mark Finch

Reputation: 766

Python 2.7 / App Engine - TypeError: is_valid() takes exactly 2 arguments (3 given)

The following code is close to what I am using without getting too long. I get the error TypeError: is_valid() takes exactly 2 arguments (3 given). To my eyes I am only passing 2 arguments. So where is the third argument coming from?

models/MyModel.py

from google.appengine.ext import db

class MyModel(db.model):
    a = db.StringProperty(required=True)
    b = db.StringProperty(required=True)
    c = db.StringProperty(required=True)

class Update:
    def is_valid(x, y)
        myquery = db.GqlQuery('SELECT * FROM Valid WHERE value = :1' x)
        v = myquery.get()

        if v.something == y:
            yet_more_stuff
            return(True)
        else:
            return(False)

controllers/WebHandler.py

import webapp2
from models.MyModel import Update

class WebHandler(webapp2.RequestHandler):
    def get(self):
        var_x = "string"
        var_y = "string"
        z = Update()
        if z.is_valid(var_x, var_y): <----- line error occurs
            do_some_stuff
        else
            do_some_other_stuff

It is probably something simple but after coding for 18 hours today my brain has turned into oatmeal.

Upvotes: 2

Views: 2216

Answers (2)

Tadeck
Tadeck

Reputation: 137420

Solution

You have two solutions:

  • add an argument representing instance in the method definition (name it self for consistency), or
  • use staticmethod decorator on the method.

Explanation and examples

This line:

def is_valid(x, y):

means that when the method is called, x is the class instance and y is the argument. If you wanted to accept two arguments (and the instance itself), your line should look like that:

def is_valid(self, x, y)

But because you are not making any actions on the instance itself, you can also use staticmethod decorator:

@staticmethod
def is_valid(x, y):

This will get rid of the instance being passed in the arguments and you will only receive the remaining arguments.

Upvotes: 5

Shay Erlichmen
Shay Erlichmen

Reputation: 31928

change the code to def is_valid(self, x, y)

Upvotes: 7

Related Questions