233

I know @ is for decorators, but what is @= for in Python? Is it just reservation for some future idea?

This is just one of my many questions while reading tokenizer.py.

3
  • 1
    See cset c553d8f72d65 (GitHub mirror...easier to read) in the CPython repo.
    – Nick T
    Dec 9, 2014 at 20:10
  • SymbolHound is a search-engine which can search on punctuation symbols. However searching on @= python doesn't currently return relevant results, because Python 3.5 documentation contains '@' but not an example of '@=' anywhere. I sent SH a message to help improve that. Python doc could improve too.
    – smci
    Mar 5, 2017 at 10:42
  • 2
    Combined with the := walrus operator of Python 3.8, you get what's known as the @:= thorny rose operator. (Or in Japan it's known as the Elvis-walrus operator.)
    – Bob Stein
    Jul 20, 2019 at 17:35

4 Answers 4

263

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.

4
  • 15
    That explains why it's in the latest version of tokenizer.py but not the 3.4 docs. Dec 9, 2014 at 18:07
  • 11
    This is covered in 3.5's docs - docs.python.org/3.5/reference/… and docs.python.org/3.5/reference/…
    – jonrsharpe
    Dec 9, 2014 at 18:09
  • 1
    Does this have conflict with Python decorators? This is not implemented in Python 2.n, right?
    – frankliuao
    Nov 2, 2017 at 2:35
  • 5
    This does not conflict decorators, because decorators may never be preceded by an expression, and binary operators must always be preceded by an expression.
    – user1804599
    Nov 4, 2017 at 11:15
120

@= 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 as A * B
  • An in-place version A @= B, used similarly as A *= 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:

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.

3
  • 23
    Just for clarification: from your first example, we could think that @ has been implemented for list, which is not the case. Jan 14, 2017 at 20:16
  • 2
    @ is associated with np.matmul, not np.dot. The two are similar but not the same.
    – Asclepius
    Sep 17, 2017 at 1:26
  • 1
    @A-B-B, perhaps you could provide an example clarifying the nuance and ensuring the answer is then complete? Apr 29, 2019 at 18:58
10

@ is the new operator for Matrix Multiplication added in Python3.5

Reference: https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465

Example

C = A @ B
0

Numpy 1.25 will support using @= as in place matrix multiplication:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.