Special Method Protocols
__rpow__() — reflected numeric protocol
Implement reflected exponentiation (**) 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. In Python 3.13, three-argument pow() does not try the reflected hook. An optional modulo argument is shown for the hook interface, but ternary fallback is not dispatched in 3.13.
Formal Syntax & Structure
python syntaxpython
def __rpow__(self, other, modulo=None):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| other | object | Other operand. |
| modulo | object | None | Optional modulus for three-argument pow(). |
Return Value
object | NotImplementedType
Operation result, or NotImplemented to decline.
Working Example
Example Snippetpython
class Number: def __init__(self, value): self.value = value def __rpow__(self, other, modulo=None): if not isinstance(other, int): return NotImplemented return pow(other, self.value, modulo) if modulo is not None else other ** self.valueprint(2 ** Number(3))