[Numpy-discussion] *= operator not intuitive

Paul Anton Letnes paul.anton.letnes@gmail....
Wed Mar 16 08:35:03 CDT 2011


Heisann!

On 16. mars 2011, at 14.30, Dag Sverre Seljebotn wrote:

> On 03/16/2011 02:24 PM, Paul Anton Letnes wrote:
>> Hi!
>> 
>> 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 [1]: a = np.ones(5)
Here, a is numpy.float64:
>>> numpy.ones(5).dtype
dtype('float64')

> In [2]: b = a
> 
> In [3]: c = a * 2
> 
> In [4]: b
> Out[4]: array([ 1.,  1.,  1.,  1.,  1.])
> 
> In [5]: 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 [6]: b
> Out[6]: 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?

Paul.


More information about the NumPy-Discussion mailing list