Why is there a list broadcasting error when using "+=" but not when using "__iadd__"?

The root cause is: when the left operand is a list and the right operand is a numpy.ndarray, the += operator does not actually call the list is __iadd__, but instead falls into the numeric addition protocol, ultimately letting the ndarray is __radd__ handle it, triggering a broadcasting error; whereas explicitly calling __iadd__ (or .extend()) goes directly through the list is sequence protocol and does not involve numpyat all.

What exactly does += do at the C level?

+= calls PyNumber_InPlaceAdd(). This function first tries the numeric protocol (the nb_inplace_add slot). Lists do not implement the numeric protocol (they do not have this slot), so it falls back to the regular addition protocol nb_add.

The problem is: lists do not have an nb_add slot, but numpy.ndarrayhas it (__radd__). So the interpreter finds the nb_add slot on the right operand and lets ndarray perform the addition. numpy naturally treats the list as an array for broadcasting, and when the shapes do not match, it throws ValueError: operands could not be broadcast together.

In this path, list.__iadd__ is completely bypassed.

Why does __iadd__ work?

x.__iadd__(y) is a direct method call, which maps to the list is sq_inplace_concat slot, which essentially calls list_inplace_concat, which in turn calls list_extend. Throughout the process, the right operand y is only treated as an iterable object, and no numeric protocol slots are checked at all, so numpy has no opportunity to intervene.

Verification in plain Python

import numpy as np

x = []
y = np.array([1, 2])

# += triggers the error
try:
   x += y   # ndarray is __radd__ takes over, broadcast error
except ValueError as e:
   print("+= error:", e)

# __iadd__ / extend works
x.__iadd__(y)
print("after __iadd__:", x)  # [1, 2]

x2 = []
x2.extend(y)
print("after extend:", x2)   # [1, 2]

So the conclusion is: += is not equivalent to __iadd__ in this scenario. When the right operand has a numeric protocol slot, += may "hijack" the operation before the list is __iadd__, whereas explicitly calling __iadd__ or .extend() bypasses all of that.

5 Views