Python augmented assignment issue
For the +
operator, Python defines three "special" methods that an object may implement:
__add__
: adds two items (+
operator). When you doa + b
, the__add__
method ofa
is called withb
as an argument.__radd__
: reflected add; fora + b
, the__radd__
method ofb
is called witha
as an instance. This is only used whena
doesn't know how to do the add and the two objects are different types.__iadd__
: in-place add; used fora += b
where the result is assigned back to the left variable. This is provided separately because it might be possible to implement it in a more efficient way. For example, ifa
is a list, thena += b
is the same asa.extend(b)
. However, in the case ofc = a + b
you have to make a copy ofa
before you extend it sincea
is not to be modified in this case. Note that if you don't implement__iadd__
then Python will just call__add__
instead.
So since these different operations are implemented with separate methods, it is possible (but generally bad practice) to implement them so they do totally different things, or perhaps in this case, only slightly different things.
Others have deduced that you're using NumPy and explained its behavior. However, you asked about the underlying implementation. Hopefully you now see why it is sometimes the case that a += b
is not the same as a = a + b
. By the way, a similar trio of methods may also be implemented for other operations. See this page for a list of all the supported in-place methods.
If array
is numpy.array
(you don't actually specify), then the issue that's happening is because these arrays cannot change their type. When you create the array without a type specifier, it guesses a type. If you then attempt to do an operation that type doesn't support (like adding it to a type with a larger domain, like complex), numpy knows perform the calculation, but it also knows that the result can only be stored in the type with the larger domain. It complains (on my machine, anyway, the first time I do such an assignment) that the result doesn't fit. When you do a regular addition, a new array has to be made in any case, and numpy gives it the correct type.
>>> a=numpy.array([1])>>> a.dtypedtype('int32')>>> b=numpy.array([1+1j])>>> b.dtypedtype('complex128')>>> a+barray([ 2.+1.j])>>> (a+b).dtypedtype('complex128')>>> a+=b>>> aarray([2])>>> a.dtypedtype('int32')>>>
The difference between a = a + b
and a += b
is, that the latter addition will, whenever possible, done “in-place” that means by changing the object a
. You can easily see this with lists.
a = b = [1, 2]a += [3]print b # [1, 2, 3]a = b = [1, 2]a = a + [3]print b # [1, 2]