Special Method Protocols

__matmul__() — numeric protocol

Implement the left operand hook for matrix multiplication (@). Return NotImplemented for an unsupported operand type to permit reflected dispatch. The example models a one-element matrix product; ordinary int objects do not support @.

Formal Syntax & Structure

python syntaxpython
def __matmul__(self, other):

Parameters & Attributes

NameTypeDescription
otherobjectOther operand.

Return Value

object | NotImplementedType

Operation result, or NotImplemented to decline.

Working Example

Example Snippetpython
class Number:    def __init__(self, value):        self.value = value    def __matmul__(self, other):        if not isinstance(other, int):            return NotImplemented        return self.value * otherprint(Number(7) @ 3)
Python Reference