简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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