232

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Octavia Togami
  • 4,186
  • 4
  • 31
  • 49
  • 1
    See cset [c553d8f72d65](https://hg.python.org/cpython/rev/c553d8f72d65) ([GitHub mirror...easier to read](https://github.com/python/cpython/commit/c8555769ea6ca6ce7718172d8e9830593ac2ae94)) in the CPython repo. – Nick T Dec 09 '14 at 20:10
  • SymbolHound is a search-engine which can search on punctuation symbols. However [searching on @= python](http://symbolhound.com/?q=%40%3D+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 05 '17 at 10:42
  • 2
    Combined with the `:=` [walrus operator](https://www.python.org/dev/peps/pep-0572/) 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 '19 at 17:35

4 Answers4

261

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 15
    That explains why it's in the latest version of tokenizer.py but not the 3.4 docs. – Octavia Togami Dec 09 '14 at 18:07
  • 11
    This is covered in 3.5's docs - https://docs.python.org/3.5/reference/simple_stmts.html#augmented-assignment-statements and https://docs.python.org/3.5/reference/expressions.html#binary-arithmetic-operations – jonrsharpe Dec 09 '14 at 18:09
  • 1
    Does this have conflict with Python decorators? This is not implemented in Python 2.n, right? – frankliuao Nov 02 '17 at 02: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. –  Nov 04 '17 at 11:15
115

@= 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.

Peque
  • 13,638
  • 11
  • 69
  • 105
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
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
amehta
  • 1,307
  • 3
  • 18
  • 22
0

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

enter image description here

Mark
  • 175
  • 1
  • 6