Special Method Protocols

__rmatmul__() — reflected numeric protocol

Implement reflected matrix multiplication (@) when the other operand declines the operation or reflected subclass priority applies. The other operand is logically on the left; preserve operand order. Return NotImplemented for unsupported types. The example models a one-element matrix product; ordinary int objects do not support @.

Formal Syntax & Structure

python syntaxpython
def __rmatmul__(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 __rmatmul__(self, other):        if not isinstance(other, int):            return NotImplemented        return other * self.valueprint(7 @ Number(3))
Python Reference