Python Decorator Self-firing
Solution 1:
@app.route("/", methods = ["GET","PUT"])
is an executable statement: it calls the route()
method of the app object. Since it's at module level, it will be executed when the script is imported.
Now, the result of calling app.route(...)
is a function, and because you've used the @
to mark it as a decorator, that function will wrap index
. Note that the syntax is just a shortcut for this:
index = app.route(...)(index)
in other words, Python will call the function returned by app.route()
with index
as a parameter, and store the result as the new index
function.
However, you're missing a level here. A normal decorator, without params, is written like this:
@foodefbar()
pass
and when the module is imported, foo()
is run and returns a function that wraps bar
. But you're calling your route()
function within the decorator call! So actually your function needs to return a decorator function that itself returns a function that wraps the original function... headscratching, to be sure.
Your route
method should look more like this:
defroute(self, *rargs, **kargs):
args = list(rargs)
if kargs:
print(kargs['methods'])
defdecorator(f):
defwrapped(index_args):
f(args[0])
return wrapped
return decorator
Solution 2:
Basically... app.route(index, "/", ["GET", "PUT"])
is a function. And this is the function which is going to be called instead of index.
In your code, when you call index()
, it calls app.route(index, "/", ["GET", "PUT"])
. This starts by printing kargs['methods']
, then creates the decorator function:
defdecorator(f):
f(args[0])
This decorator will call the decorated function (f
) with one argument, args[0]
, which here is "/". This prints route: /
.
The best explanation of decorators I've found is here: How to make a chain of function decorators?
If you dont want the self-firing, you can define your decorator this way:
defroute(*rargs, **kargs):
args = list(rargs)
if kargs:
print(kargs['methods'])
defdecorator(f):
f(args[0])
return decorator
@app.route("/", methods = ["GET","PUT"])defindex(rt):
print('route: ' + rt)
However, the rt
argument of index will never be used, because route
always calls index
with args[0]
which is always \
...
Post a Comment for "Python Decorator Self-firing"