Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 800
Mark Finch Avatar asked Aug 15 '26 09:08

Mark Finch


2 Answers

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

like image 188
Shay Erlichmen Avatar answered Aug 16 '26 22:08

Shay Erlichmen


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.

like image 32
Tadeck Avatar answered Aug 16 '26 23:08

Tadeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!