[Numpy-discussion] Response to PEP suggestions

Tim Hochberg tim.hochberg at cox.net
Thu Feb 17 14:45:09 CST 2005


Robert Kern wrote:

> Fernando Perez wrote:
>
>> Robert Kern wrote:
>>
>>> Why not just do
>>>
>>>    b = ravel(a)
>>>
>>> ?
>>>
>>> That should work in both cases just fine.
>>
>>
>>
>> Because I thouhgt that ravel would make a copy regardless.  But I may 
>> be wrong, much of this is very old code, when I was just picking up 
>> Numeric.  At the time, I felt it might be best to avoid the 
>> unnecessary copies if possible.
>
>
> Nope.
>
> In [1]: a = arange(8)
>
> In [2]: a.shape = (2,4)
>
> In [3]: a
> Out[3]: NumPy array, format: long
> [[0 1 2 3]
>  [4 5 6 7]]
>
> In [4]: ravel(a)[3] = 10
>
> In [5]: a
> Out[5]: NumPy array, format: long
> [[ 0  1  2 10]
>  [ 4  5  6  7]]


Ick, that's horrible. Functions that sometimes copy and sometimes don't 
are generally bad news IMO. This is just a way to introduce nasty, 
invisible bugs. The exceptions are things like asarray that are explicit 
about their variable behaviour.

I'd be much happier if flat never made copies, but always worked by some 
sort of deep juju, while ravel always made copies.

-tim






More information about the Numpy-discussion mailing list