Mar*_*nch 2 python google-app-engine typeerror python-2.7
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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
It is probably something simple but after coding for 18 hours today my brain has turned into oatmeal.
你有两个解决方案:
self
为一致性)或staticmethod
在方法上使用装饰器.这一行:
def is_valid(x, y):
Run Code Online (Sandbox Code Playgroud)
表示调用方法时,x
是类实例并且y
是参数.如果你想接受两个参数(和实例本身),你的行应该是这样的:
def is_valid(self, x, y)
Run Code Online (Sandbox Code Playgroud)
但是因为你没有对实例本身做任何操作,你也可以使用staticmethod
装饰器:
@staticmethod
def is_valid(x, y):
Run Code Online (Sandbox Code Playgroud)
这将删除在参数中传递的实例,您将只接收剩余的参数.