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

Ryan May rmay@ou....
Mon Jan 7 09:47:04 CST 2008


Stuart Brorson wrote:
>>> I realize NumPy != Matlab, but I'd wager that most users would think
>>> that this is the natural behavior......
>> Well, that behavior won't happen. We won't mutate the dtype of the array because
>> of assignment. Matlab has copy(-on-write) semantics for things like slices while
>> we have view semantics. We can't safely do the reallocation of memory [1].
> 
> That's fair enough.  But then I think NumPy should consistently
> typecheck all assignmetns and throw an exception if the user attempts
> an assignment which looses information.
> 

Yeah, there's no doubt in my mind that this is a bug, if for no other
reason than this inconsistency:

>>> import numpy as N
>>> a = N.zeros((2,2))
>>> a[0,0]=3+4j
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float; use abs(z)
>>> b=N.array([3+4j])
>>> a[0,0]=b[0]
>>> a
array([[ 3.,  0.],
       [ 0.,  0.]])

I've been bitten by this myself and would have very much appreciated an
exception rather than incorrect answers.  It would seem that this
behavior violates "In the face of ambiguity, refuse the temptation to
guess."

The question to me is how much do people rely on this "feature" of the
API and does that dictate putting the change off until Numpy 1.1 or beyond?

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma


More information about the Numpy-discussion mailing list