1

I have been working with Flask.route() decorator for a while and wanted to write my own, but it always tells me that the function isn't passed into it.

I've copied everything exactly like in the Flask examples, so my decorator definition must be wrong:

def decorator(f, *d_args):
    def function(*args, **kwargs):
        print('I am decorated')
        return f(*args, **kwargs)

    return function


@decorator()
def test(a, b=1):
    print('Test', a, b)


test(1, 6)

The error I get:

Traceback (most recent call last):
  File "C:/Users/Tobi/Desktop/decorators.py", line 49, in <module>
    @decorator()
TypeError: decorator() missing 1 required positional argument: 'f'

1 Answers1

1

First of all, there are questions on SO which handle this problem. You should have done more research on this error before writing a new question. But anyway:

The reason for your error is because you are calling the decorator by writing it before a def because you are already calling it using the brackets decorator() without passing anything in, it throws an error.

For your decorator, the correct usage would be:

@decorator # no brackets here
def function()
    ...
TheClockTwister
  • 819
  • 8
  • 21