Operators

@ — Matrix multiplication

Requests matrix multiplication or another operation explicitly defined by the operand types. Python supplies the syntax but no ordinary built-in numeric or sequence type implements matrix multiplication. Libraries and user-defined objects can implement __matmul__ and __rmatmul__; when neither handles the operands, TypeError is raised. It groups left to right at the same precedence as *. A custom implementation must define its own shape and dimension rules.

Formal Syntax & Structure

python syntaxpython
left @ right

Parameters & Attributes

NameTypeDescription
leftobjectObject implementing matrix multiplication or supporting reflected dispatch.
rightobjectCompatible operand; dimensions are defined by its implementation.

Return Value

object

Result selected by the operand types and their operation implementations.

Working Example

Example Snippetpython
class Row:    def __init__(self, values):        self.values = values    def __matmul__(self, column):        if len(self.values) != len(column):            raise ValueError("length mismatch")        return sum(a * b for a, b in zip(self.values, column))
print(Row([2, 3]) @ [4, 5])
Python Reference