Special Method Protocols

__iadd__() — in place numeric protocol

Handle augmented assignment with += for addition. 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.

Formal Syntax & Structure

python syntaxpython
def __iadd__(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 __iadd__(self, other):        if not isinstance(other, int):            return NotImplemented        self.value = self.value + other        return self
def apply():    x = Number(7)    alias = x    x += 3    return x.value, x is aliasprint(apply())
Python Reference