[Numpy-discussion] Nasty bug using pre-initialized arrays

Stuart Brorson sdb at cloud9.net
Fri Jan 4 16:30:25 EST 2008


> I just discovered this today.  It looks like a bug to me.  Please
> flame me mercilessly if I am wrong!  :-)

Hmmmm.... after a little more playing around, I think it's indeed true
that NumPy does a typecast to make the resulting assignment have the
same type as the LHS, regardless of the type of the RHS.  Below I
attach another example, which shows this behavior.

As a naive user, I would not expect that my variables would get
silently typecast in an assignment like this.  But is this behavior
Pythonic?  I'm not sure.....  Normally, the Pythonic thing to do when
assigning non-conforming variables is to barf -- throw an exception.
At least that's what I would expect.

Comments?

Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA
http://www.interactivesupercomputing.com/


-----------------------  <session log>  ---------------------

In [77]: A = 10*numpy.random.rand(4)

In [78]: B = numpy.zeros((4))

In [79]: B.dtype='int64'

In [80]:

In [80]: for i in range(4):
    ....:       B[i] = A[i]
    ....:

In [81]: A
Out[81]: array([ 1.71327285,  3.48128855,  7.51404178,  8.96775254])

In [82]: B
Out[82]: array([1, 3, 7, 8], dtype=int64)

-----------------------  </session log>  ---------------------



More information about the NumPy-Discussion mailing list