具有多个参数的 python 装饰函数(Tornado)

标签 python arguments decorator tornado

我编写了一个装饰器来验证调用。仅使用一个参数时它可以正常工作,但使用更多参数时就不行了,触发 inner() 恰好需要 1 个参数(给定 2 个)。由于我使用 Tornado,所以我有一些回调意大利面条,但我不确定执行此操作的最佳方法是什么。

#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)

编辑:

将代码更新为工作版本。

谢谢!

最佳答案

我一开始以为问题很简单,但后来你发布了一个与原始错误消息相矛盾的回溯。然而,我认为问题仍然非常简单,假设回溯错误是正确的。回想一下:

@decorator
def foo(x):
    return x + 1

只是语法糖:

def foo(x):
    return x + 1
foo = oauth_machine.auth(foo)

因此,当您在 get 上使用 @oauth_machine.auth 时,它会通过闭包作为 fn 传递到 inner 中>.

def auth(fn):
    def inner(self):
        res = get_user_by_credentials(self, fn, callback=done_auth)
    return inner

然后它再次作为 fn 传递到 get_user_by_credentials,这又产生另一个闭包,该闭包将 fn 传递给 callback

def get_user_by_credentials(self, fn, callback):

    def onFetchUserCredentials(result, error):
        self.user = result
        callback(self, fn)

callback 被定义为 inner 中的 done_auth,因此意味着 fn (即原来的 get) 被传递到那里,然后调用 result:

def done_auth(result, fn):
    return fn(result)

但是fn(即get)需要两个参数。您只传递一个,导致错误。

关于具有多个参数的 python 装饰函数(Tornado),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426793/

相关文章:

python - 302 和 urllib2 丢失 cookie

python - 使用自定义信息填写日历

python - Leetcode 的两个和错误 __init__() 缺少 2 个位置参数

python - 文档字符串应该放在装饰器之前还是之后?

python - 为什么 `joblib.delayed` 不能用作装饰器?

python - 这些类型的 python 装饰器是如何编写的?

python - 使任何其他模块都可以访问变量

python - 将ast.Num转换为decimal.Decimal以获得python中的精度

c# - 寻找 .NET 的命令行参数解析器

java - 没有值(value)的Java选项-D是什么意思?