0

While reading through the Python 3 Lexical analysis, I became slightly confused by the last section on operators[1] and delimiters. The @ character is listed as both an operator and a delimiter, and @= is also listed as an augmented assignment operator. Following the form of other augmented assignment operators, I would expect this to mean that the @ character can be used like so:

x = x @ y

or

x @= y

I have tried using it in this way with integers and strings without any success. I am familiar with using @ for decorators, but fail to see how an augmented assignment operator is compatible with decorators.

What is the purpose of @ and @= when used as an operator and/or delimiter in Python 3?


[1] Python 3 - Operators: https://docs.python.org/3/reference/lexical_analysis.html#operators

  • as an operator it's a matrix multiply. `a = np.arange(4).reshape(2, 2); b = np.arange(4).reshape(2, 2); print(a @ b)` shows `array([[ 2, 3], [ 6, 11]])`. I've only seen it work with numpy arrays though. – Elliot Nov 23 '16 at 04:11
  • @Elliot you do appear to be correct, but I'd love to know if this works outside of numpy. Using your example, I tried `a @= b` and received the following error: `TypeError: In-place matrix multiplication is not (yet) supported. Use 'a = a @ b' instead of 'a @= b'.` – Sam Costigan Nov 23 '16 at 05:05

1 Answers1

1

Expressions states

The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator.

So you have to use numpy or other math libraries to make x = x @ y work.

Gennady Kandaurov
  • 1,914
  • 1
  • 15
  • 19