[Numpy-discussion] *= operator not intuitive

Dag Sverre Seljebotn d.s.seljebotn@astro.uio...
Wed Mar 16 08:30:58 CDT 2011


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)

In [2]: b = a

In [3]: c = a * 2

In [4]: b
Out[4]: array([ 1.,  1.,  1.,  1.,  1.])

In [5]: a *= 2

In [6]: b
Out[6]: array([ 2.,  2.,  2.,  2.,  2.])


-- Dag


More information about the NumPy-Discussion mailing list