简体   繁体   English

龙卷风的自定义装饰器

[英]Custom Decorators in Tornado

I'm facing an error while trying to make a custom tornado decorator. 尝试制作自定义龙卷风装饰器时遇到错误。

TypeError: post() missing 1 required positional argument: 'self'

The sample code is: 示例代码是:

def decorate( function_name ):
    # Do something
    function_name()
    # Do something

class MainHandler( tornado.web.RequestHandler ):
    @decorate
    def post( self ):
        # Do whatever

How do I pass the context of self to the decorator ? 如何将self的上下文传递给装饰器?

It seems you're not passing the arguments from the decorator to the decorated method. 似乎您没有将参数从装饰器传递给装饰的方法。

Here's how your decorator should look like: 这是您的装饰器的外观:

def decorate(func):
    def wrapper(*args, **kwargs):
        # pass the received arguments to
        # the decorated function
        return func(*args, **kwargs)
    return wrapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM