15

i ran into something interesting about the python augmented assignment +=

it seems to be automatic data type conversion is not always done for a += b if a is a 'simpler' data type, while a = a + b seems to work always

cases where the conversion is done

a = 1
b = 1j

a = 1
b = 0.5

case where the conversion is not done

from numpy import array
a = array([0, 0 ,0])
b = array([0, 0, 1j])

after a += b, a remains as integer matrix, instead of complex matrix

i used to think a += b is the same as a = a + b, what is the difference of them in the underlying implementation?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
nos
  • 19,875
  • 27
  • 98
  • 134
  • what does `array` in your example refer to? Is that from the builtin `array` module? if so, your example doesn't even work, since there's no typecode... – SingleNegationElimination Dec 07 '10 at 22:49
  • `a = array([0, 0 ,0])` and `b = array([0, 0, 1j])` don't work with the `array` class in the module of the same name. They're both missing an initial *typecode* argument. And, AFAIK, the class doesn't support complex numbers nor `+=` augmented assignment. So I don't understand what you're asking here. – martineau Dec 07 '10 at 22:50
  • @martineau See my comment on Rafe's answer (now deleted.) referencing this NumPy question: http://www.scipy.org/FAQ#head-1ed851e9aff803d41d3cded8657b2b15a888ebd5 – ACoolie Dec 07 '10 at 22:52
  • Assuming ACoolie and Manux are correct (that NumPy arrays are being used), the question should be edited to reflect that. I've gone ahead and retagged, as that is all I have the rep to do. – John Y Dec 07 '10 at 22:54

4 Answers4

16

For the + operator, Python defines three "special" methods that an object may implement:

  • __add__: adds two items (+ operator). When you do a + b, the __add__ method of a is called with b as an argument.
  • __radd__: reflected add; for a + b, the __radd__ method of b is called with a as an instance. This is only used when a doesn't know how to do the add and the two objects are different types.
  • __iadd__: in-place add; used for a += 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, if a is a list, then a += b is the same as a.extend(b). However, in the case of c = a + b you have to make a copy of a before you extend it since a 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.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 1
    I should note that in the example I gave for lists, `a += b` does in fact have different results from `a = a + b`. If `a` is known by another name, that other name will "see" the extended list after `a += b` but not after `a = a + b`. – kindall Dec 26 '10 at 16:49
8

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.dtype
dtype('int32')
>>> b=numpy.array([1+1j])
>>> b.dtype
dtype('complex128')
>>> a+b
array([ 2.+1.j])
>>> (a+b).dtype
dtype('complex128')
>>> a+=b
>>> a
array([2])
>>> a.dtype
dtype('int32')
>>> 
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

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]
nils
  • 628
  • 3
  • 8
  • Yes, and in the case of numpy arrays, that means keeping the type of `a` the same, so complex numbers will not be represented. – tkerwin Dec 07 '10 at 22:55
0

Rafe Kettler's answer is correct, but it seems you have managed to get a=[0,0,0] after adding it to b (according to your post).

Well if you're using numpy or scipy (I say this because I see array and wonder what array is being created here), then this is "normal", and should even raise a warning:

ComplexWarning: Casting complex values to real discards the imaginary part

Manux
  • 3,643
  • 4
  • 30
  • 42