<div dir="auto"><div><div class="gmail_quote"><div dir="ltr">On Wed, Dec 26, 2018, 18:29 Sebastian Berg <<a href="mailto:sebastian@sipsolutions.net">sebastian@sipsolutions.net</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
In <a href="https://github.com/numpy/numpy/pull/11897" rel="noreferrer noreferrer" target="_blank">https://github.com/numpy/numpy/pull/11897</a> I am looking into the<br>
addition of a `copy=np.never_copy` argument to:<br>
  * np.array<br>
  * arr.reshape/np.reshape<br>
  * arr.astype<br>
<br>
Which would cause an error to be raised when numpy cannot guarantee<br>
that the returned array is a view of the input array.<br>
The motivation is to easier avoid accidental copies of large data, or<br>
ensure that in-place manipulation will be meaningful.<br>
<br>
The copy flag API would be:<br>
  * `copy=True` forces a copy<br>
  * `copy=False` allows numpy to copy if necessary<br>
  * `copy=np.never_copy` will error if a copy would be necessary<br>
  * (almost) all other input will be deprecated.<br>
<br>
Unfortunately using `copy="never"` is tricky, because currently<br>
`np.array(..., copy="never")` behaves exactly the same as<br>
`np.array(..., copy=bool("never"))`. So that the wrong result would be<br>
given on old numpy versions and it would be a behaviour change. <br>
<br>
Some things that are a not so nice maybe:<br>
 * adding/using `np.never_copy` is not very nice<br>
 * Scalars need a copy and so will not be allowed<br>
 * For rare array-likes numpy may not be able to guarantee no-copy,<br>
   although it could happen (but should not).<br>
<br>
<br>
The history is that a long while ago I considered adding a copy flag to<br>
`reshape` so that it is possible to do `copy=np.never_copy` (or<br>
similar) to ensure that no copy is made. In these, you may want<br>
something like an assertion:<br>
<br>
```<br>
new_arr = arr.reshape(new_shape)<br>
assert np.may_share_memory(arr, new_arr)<br>
<br>
# Which is sometimes -- but should not be -- written as:<br>
arr.shape = new_shape  # unnecessary container modification<br>
<br>
# Or:<br>
view = np.array(arr, order="F")<br>
assert np.may_share_memory(arr, new_arr)<br>
```<br>
<br>
but is more readable and will not cause an intermediate copy on error.<br>
<br>
<br>
So what do you think? Other variants would be to not expose this for<br>
`np.array` and probably limit `copy="never"` to the reshape method. Or<br>
just to not do it at all. Or to also accept "never" for `reshape`,<br>
although I think I would prefer to keep it in sync and wait for a few<br>
years to consider that.<br>
<br>
Best,<br>
<br>
Sebastian<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Could this approach be used to deprecate `ravel` and let us just use `flatten`?</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>