Search code examples
pythontornadodecoratorpython-decorators

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 ?


Solution

  • 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