> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Matrix Multiplication Assignment

The `@=` 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.

```python theme={"dark"}
A @= B
```

## 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:

1. **`__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 identifier `A`.
2. **Fallback to `A @ B`:** If `__imatmul__` is not implemented or returns `NotImplemented`, Python falls back to evaluating the standard matrix multiplication (`A @ B`) and assigns the newly created object back to `A`. 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 invoke `B.__rmatmul__(A)` *before* attempting `A.__matmul__(B)`.
   * **`__matmul__` (Standard Matrix Multiplication):** If the subclass rule does not apply, Python invokes `A.__matmul__(B)`.
   * **`__rmatmul__` (Reflected Matrix Multiplication):** If `A` does not implement `__matmul__`, or if `A.__matmul__(B)` returns `NotImplemented`, Python invokes `B.__rmatmul__(A)`.
3. **`TypeError`:** If none of these methods are implemented, or if all attempted methods return `NotImplemented`, Python raises a `TypeError` indicating 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__`.

```python theme={"dark"}
class MatrixObject:
    def __init__(self, data):
        self.data = data

    def __imatmul__(self, other):
        if not isinstance(other, MatrixObject):
            return NotImplemented
        
        # Logic for in-place matrix multiplication goes here
        # Example mutates self.data directly
        self.data = self._compute_matrix_product(self.data, other.data)
        
        # Must return self for augmented assignment to work correctly
        return self

    def __matmul__(self, other):
        if not isinstance(other, MatrixObject):
            return NotImplemented
            
        # Logic for standard matrix multiplication
        new_data = self._compute_matrix_product(self.data, other.data)
        return MatrixObject(new_data)
        
    def _compute_matrix_product(self, data1, data2):
        # Internal computation logic
        pass


# Execution
X = MatrixObject(...)
Y = MatrixObject(...)

X @= Y  # Invokes X.__imatmul__(Y)
```

## 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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
