나는 전화를 인증하기 위해 데코레이터를 작성했다. 하나의 인수만으로도 제대로 작동하지만 더 많은 인수를 사용하면 inner() takes exactly 1 argument (2 given)
을 트리거합니다. 나는 토네이도를 사용하고 있기 때문에 약간의 콜백 스파게티가 있지만, 이것을하기에 가장 좋은 방법은 무엇인지 모르겠습니다.여러 args (토네이도)와 파이썬 장식 기능
#this works
class FirstHandler(BaseHandler):
@asynchronous
@oauth_machine.auth
def post(self):
print self.user
self.finish()
#this now also does
class SecondHandler(BaseHandler):
@asynchronous
@oauth_machine.auth
def get(self, args):
self.write("ok")
self.finish()
데코레이터 기능 (들)
def auth(fn):
def inner(self, *args):
res = get_user_by_credentials(self, fn, args, callback=done_auth)
return inner
def get_user_by_credentials(self, fn, callback):
def onFetchUserCredentials(result, error):
self.user = result
callback(self, fn, args)
email = self.get_argument("email")
password = self.get_argument("password")
settings.DB.users.find_one({'email': email, 'password': password }, callback=onFetchUserCredentials)
def done_auth(result, fn, args):
return fn(result, args)
편집 : 버전을 작업하는
업데이트 코드입니다.
감사합니다.
'def inner (* args) :'와'print args'를'def inner (self) :'로 변경하면 전달되는 인자를 확인할 수 있습니다. – Blender
함수에서 전달 된 두 개의 인수를 얻습니다. 'self '를'args [0]'으로 변경함으로써,'get()은 정확히 2 개의 인수 (주어진 1 개)'를 얻습니다. 두 번째 논의를 어디에서 지나치지 않습니까? –
'get '을 호출하는 코드가 없습니다. 관련 코드를 모두 게시했다고 가정하면, 이는 토네이도 내부에서 부적절하게'get '이라고 부르는 것입니다. 이는 아마도 토네이도에서 부적절하게 호출하는 것을 의미합니다. 우리가 볼 수 있도록 스택 추적을 게시하십시오. – abarnert