Flask : What exactly is @app
It is a decorator. When decorated by @app.route('/')
(which is a function), calling index()
becomes the same as calling app.route('/')(index)()
.
Here is another link that can explain it, in the python wiki.
The @
is telling Python to decorate the function index()
with the decorator defined in app.route()
.
Basically, a decorator is a function that modifies the behaviour of another function. As a toy example, consider this.
def square(func):
def inner(x):
return func(x) ** 2
return inner
@square
def dbl(x):
return x * 2
Now - calling dbl(10)
will return not 20
, as you'd expect but 400
(20**2
) instead.
This is a nice step-by-step. explanation of decorators.