1
Several times I came across snippets of python code that used "@" as in flask:
import flask
App = flask.Flask(__name__)
@App.route("/")
Why not just App.route
?
1
Several times I came across snippets of python code that used "@" as in flask:
import flask
App = flask.Flask(__name__)
@App.route("/")
Why not just App.route
?
2
The "@" prefix of an expression that is a line above a function or class statement is a "decorator" (Decorator) . What they are and how decorators work is explained in the question that was marked as a suggestion that this would be a duplicate: How Python Decorators Work?
@
as a decorator:
@modificar_funcao
def minha_funcao():
...
However, I reopened the question, why is there another use of @
that is not addressed in that question: it can be used with an operator that uses two operands (as well as + - * /
) - and in this context, it indicates a "multiplication of matrices". The matrix multiplication operation is not defined in any type that accompanies a standard Python instance, so it is little known - but the "numpy", the main library with Python matrix operations uses the "@" in that sense. Also, any class created in your program can define an operation with "@" as an operator if you implement the method __matmul__
.
@
as a matrix multiplication operator:
In [7]: import numpy as np
In [8]: A = np.ones((2,2))
In [9]: B = np.ones((2,1))
In [12]: A[0,0] = 3
In [13]: B[0, 0] = 5
In [14]: A
Out[14]:
array([[3., 1.],
[1., 1.]])
In [15]: B
Out[15]:
array([[5.],
[1.]])
In [16]: A @ B
Out[16]:
array([[16.],
[ 6.]])
Browser other questions tagged python flask
You are not signed in. Login or sign up in order to post.
That answers your question ? How Python Decorators Work?
– JeanExtreme002