on-internet
← Community

Why there is an error with list broadcasting when using "+=", but not when using "__iadd__"?

Stack Overflow ·

Why there is an error about broadcasting? ValueError Traceback (most recent call last) Cell In[7], line 3 1 import numpy as np 2 x = [] ----> 3 x += np.array([1, 2]) ValueError: operands could not be broadcast together with shapes (0,) (2,) The real puzzle is not that x += y raises an error by itself, but that it behaves differently than x = x.__iadd__(y). According to the data model docs: ... if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) Since list.__iadd__ exists and does not return NotImplemented, why does x += y raise an error while x =

Comments

Sign in or sign up to comment and upvote.

No comments yet.