[Numpy-discussion] Warn or immidiately change readonly flag on broadcast_arrays return value?

Marten van Kerkwijk m.h.vankerkwijk at gmail.com
Tue Dec 25 11:32:27 EST 2018


Hi Juan,

I also use `broadcast_to` a lot, to save memory, but definitely have been
in a situation where in another piece of code the array is assumed to be
normal and writable (typically, that other piece was also written by me; so
much for awareness...). Fortunately, `broadcast_to` already returns
read-only views (it is newer and was designed as such from the start), so I
was alerted.

At the time `broadcast_to` was introduced [1], there was a discussion about
`broadcast_arrays` as well, and it was decided to leave it writable - see
the note at
https://github.com/numpy/numpy/blob/master/numpy/lib/stride_tricks.py#L265.
The same note suggests a deprecation cycle, but like Hameer I don't really
see how that helps - there is no way to avoid the warning in the large
majority of cases where readonly views are exactly what the user wanted in
the first place. So, that argues for cold turkey...

The one alternative would be to actually warn only if one attempts to write
to a broadcast array - apparently this has been done for `np.diag` as well
[2], so code to do so supposedly exists.

All the best,

Marten

[1] https://github.com/numpy/numpy/pull/5371
[2] https://github.com/numpy/numpy/pull/5371#issuecomment-75238827
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20181225/38f0361b/attachment.html>


More information about the NumPy-Discussion mailing list