[Numpy-discussion] *= operator not intuitive
Paul Anton Letnes
Wed Mar 16 08:35:03 CDT 2011
On 16. mars 2011, at 14.30, Dag Sverre Seljebotn wrote:
> On 03/16/2011 02:24 PM, Paul Anton Letnes wrote:
>> This little snippet of code tricked me (in a more convoluted form). The *= operator does not change the datatype of the left hand side array. Is this intentional? It did fool me and throw my results quite a bit off. I always assumed that 'a *= b' means exactly the same as 'a = a * b' but this is clearly not the case!
> In : a = np.ones(5)
Here, a is numpy.float64:
> In : b = a
> In : c = a * 2
> In : b
> Out: array([ 1., 1., 1., 1., 1.])
> In : a *= 2
So since a is already float, and b is the same object as a, the resulting a and b are of course floats.
> In : b
> Out: array([ 2., 2., 2., 2., 2.])
This is not the case I am describing, as in my case, a was of dtype integer.
Or did I miss something?
More information about the NumPy-Discussion