TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@= operator is the augmented assignment operator for matrix multiplication in Python, introduced in Python 3.5 via PEP 465. It evaluates the matrix product of two objects and assigns the resulting value back to the left-hand operand.
Underlying Mechanics
When the Python interpreter encounters the@= operator, it attempts to perform an in-place operation by invoking specific magic methods (dunder methods) in a strict resolution order.
The evaluation of A @= B follows these steps:
__imatmul__(In-place Matrix Multiplication): Python first checks if the left operand (A) implements the__imatmul__(self, other)method. If implemented, this method is executed. It is expected to modify the object in-place and return the mutated object, which is then bound back to the identifierA.- Fallback to
A @ B: If__imatmul__is not implemented or returnsNotImplemented, Python falls back to evaluating the standard matrix multiplication (A @ B) and assigns the newly created object back toA. This fallback adheres to Python’s standard binary operator resolution:- The Subclass Rule: If the right operand (
B) is an instance of a strict subclass of the left operand’s (A) class, Python prioritizes the right operand’s reflected method. It will invokeB.__rmatmul__(A)before attemptingA.__matmul__(B). __matmul__(Standard Matrix Multiplication): If the subclass rule does not apply, Python invokesA.__matmul__(B).__rmatmul__(Reflected Matrix Multiplication): IfAdoes not implement__matmul__, or ifA.__matmul__(B)returnsNotImplemented, Python invokesB.__rmatmul__(A).
- The Subclass Rule: If the right operand (
TypeError: If none of these methods are implemented, or if all attempted methods returnNotImplemented, Python raises aTypeErrorindicating unsupported operand types.
Implementation Syntax
To support the@= operator in custom classes, you must define the __imatmul__ method. If you want to support the fallback behavior, you should also define __matmul__ and __rmatmul__.
Type Support
The@= operator is not implemented by any of Python’s built-in scalar or collection types (such as int, float, list, or dict). Attempting to use @= on these standard types will result in a TypeError. The operator exists purely at the syntax level in standard Python to provide a standardized API for third-party numerical and scientific libraries, such as NumPy.
Master Python with Deep Grasping Methodology!Learn More





