2 ms·
Exactly. In particular, the decorator can be an expression, so @decorator_factory(foo, bar) def function(): print("Hello World!") becomes def fun
by geofft 12y ago
Exactly. In particular, the decorator can be an expression, so
@decorator_factory(foo, bar)
def function():
print("Hello World!")
becomes
def function():
print("Hello World!")
function = decorator_factory(foo, bar)(function)
or in this case, app.route(path)(function). So route() needs to return something that can be called with one argument. That's executed immediately at the time the function is defined.
Also the syntactic sugar lets you do stupid things like
>>> @print
... def f():
... pass
...
<function f at 0x101cfdde8>
>>> repr(f)
'None'
because print is a function that has a side effect and returns None. (If you're on Python 2, don't forget to `from __future__ import print_function`)