From the documentation:
The
@(at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator.
The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__, __rmatmul__ or __imatmul__ similar to how + and += map to __add__, __radd__ or __iadd__.
The operator and the rationale behind it are discussed in detail in PEP 465.
Answer from user1804599 on Stack OverflowFrom the documentation:
The
@(at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator.
The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__, __rmatmul__ or __imatmul__ similar to how + and += map to __add__, __radd__ or __iadd__.
The operator and the rationale behind it are discussed in detail in PEP 465.
@= and @ are new operators introduced in Python 3.5 performing matrix multiplication. They are meant to clarify the confusion which existed so far with the operator * which was used either for element-wise multiplication or matrix multiplication depending on the convention employed in that particular library/code. As a result, in the future, the operator * is meant to be used for element-wise multiplication only.
As explained in PEP0465, two operators were introduced:
- A new binary operator
A @ B, used similarly asA * B - An in-place version
A @= B, used similarly asA *= B
Matrix Multiplication vs Element-wise Multiplication
To quickly highlight the difference, for two matrices:
A = [[1, 2], B = [[11, 12],
[3, 4]] [13, 14]]
Element-wise multiplication will yield:
A * B = [[1 * 11, 2 * 12], [3 * 13, 4 * 14]]Matrix multiplication will yield:
A @ B = [[1 * 11 + 2 * 13, 1 * 12 + 2 * 14], [3 * 11 + 4 * 13, 3 * 12 + 4 * 14]]
Usage in Numpy
So far, Numpy used the following convention:
the
*operator (and arithmetic operators in general) were defined as element-wise operations on ndarrays and as matrix-multiplication on numpy.matrix type.method/function
dotwas used for matrix multiplication of ndarrays
Introduction of the @ operator makes the code involving matrix multiplications much easier to read. PEP0465 gives us an example:
# Current implementation of matrix multiplications using dot function
S = np.dot((np.dot(H, beta) - r).T,
np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r))
# Current implementation of matrix multiplications using dot method
S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)
# Using the @ operator instead
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
Clearly, the last implementation is much easier to read and interpret as an equation.
What does the "@" operator do in Python?
python - What does the @-operator do? - Blender Stack Exchange
syntax - What does the "at" (@) symbol do in Python? - Stack Overflow
numpy - What's the difference between @ and * with python matrix multiplication? - Stack Overflow
Videos
I know in MATLAB it has something to do with function handles but I hadn't really ever used it in MATLAB either. You can see an example in this video: https://www.youtube.com/watch?v=Oi4SJqJIL2E. It's the line:
X = R @ np.diag(sig) @ no.random.randn(2, nPoints) ...
An @ symbol at the beginning of a line is used for class and function decorators:
PEP 318: Decorators
Python Decorators - Python Wiki
The most common Python decorators are:
@property@classmethod@staticmethod
An @ in the middle of a line is probably matrix multiplication:
@as a binary operator.
Example
class Pizza(object):
def __init__(self):
self.toppings = []
def __call__(self, topping):
# When using '@instance_of_pizza' before a function definition
# the function gets passed onto 'topping'.
self.toppings.append(topping())
def __repr__(self):
return str(self.toppings)
pizza = Pizza()
@pizza
def cheese():
return 'cheese'
@pizza
def sauce():
return 'sauce'
print pizza
# ['cheese', 'sauce']
This shows that the function/method/class you're defining after a decorator is just basically passed on as an argument to the function/method immediately after the @ sign.
First sighting
The microframework Flask introduces decorators from the very beginning in the following format:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
This in turn translates to:
rule = "/"
view_func = hello
# They go as arguments here in 'flask/app.py'
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
pass
Realizing this finally allowed me to feel at peace with Flask.