[Numpy-discussion] copy on demand
tim.hochberg at ieee.org
Fri Jun 14 09:14:05 CDT 2002
<"Perry Greenfield" writes>
> Numarray already returns a view of the array if it is contiguous.
> Copies are only produced if it is non-contiguous. I assume that
> is the behavior you are asking for?
This is one horrible aspect of NumPy that I hope you get rid of. I've been
burned by this several times -- I expected a view, but silently got a copy
because my array was noncontiguous. If you go with copy semantics, this will
go away, if you go with view semantics, this should raise an exception
instead of silently copying. Ditto with reshape, etc. In my experience,
this is a source of hard to find bugs (as opposed to axes issues which tend
to produce shallow bugs).
> Currently for numarray .flat will fail if it isn't contiguous. It isn't
> clear if this should change. If .flat is meant to be a view always, then
> it should always fail it the array is not contiguous. Ravel is not
> guaranteed to be a view.
Ravel should either always return a view or always return a copy -- I don't
> This is a problematic issue if we decide to switch from view to copy
> semantics. If slices produce copies, then does .flat? If so, then
> how does one produce a flattened view? x.view.flat?
Wouldn't that just produce a copy of the view? Unless you did some weird
special casing on view? The following would work, although it's a little
flat_x = x.view[:] # Or however "get me a view" would be spelled.
flat_x.shape = (-1,)
More information about the Numpy-discussion