Special Method Protocols

__ipow__() — in place numeric protocol

Handle augmented assignment with **= for exponentiation. The hook may mutate the receiver or return a different object; assignment binds the returned result. If absent or returning NotImplemented, Python tries the ordinary binary operation. The example intentionally mutates a wrapper. The augmented expression passes only the exponent; the data model also lists an optional modulo parameter.

Formal Syntax & Structure

python syntaxpython
def __ipow__(self, other):

Parameters & Attributes

NameTypeDescription
otherobjectOther operand.

Return Value

object | NotImplementedType

Result assigned back to the target, or NotImplemented to decline.

Working Example

Example Snippetpython
class Number:    def __init__(self, value):        self.value = value    def __ipow__(self, other):        if not isinstance(other, int):            return NotImplemented        self.value = self.value ** other        return self
def apply():    x = Number(2)    alias = x    x **= 3    return x.value, x is aliasprint(apply())
Python Reference