This is due to the difference between __add__
and __iadd__
methods
However usually the difference is seen between mutable vs immutable objects
>>> x = np.repeat([1], 10)
>>> y = np.random.random(len(x))
>>> x += y
>>> x
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
contrast with
>>> x = np.repeat([1.0], 10)
>>> x += y
>>> x
array([ 1.05192255, 1.00844068, 1.27569982, 1.40997015, 1.17270114,
1.27335121, 1.70719855, 1.72778867, 1.64679031, 1.23241938])
so __iadd__
is causing the addition to be truncated back to int when x is int type
This makes sense if you think about it - x can't magically change the type of it's elements (where would it store those extra bytes)
for __add__
there is no problem producting a new array of floats instead of ints