[Numpy-discussion] Views and Increments

Stephan Hoyer shoyer at gmail.com
Mon Aug 8 16:08:46 EDT 2016


On Mon, Aug 8, 2016 at 6:11 AM, Anakim Border <akborder at gmail.com> wrote:

> Alternative version:
>
> >>> a = np.arange(10)
> >>> a[np.array([1,6,5])] += 1
> >>> a
> array([0, 2, 2, 3, 4, 6, 7, 7, 8, 9])
>

I haven't checked, but a likely explanation is that Python itself
interprets a[b] += c as a[b] = a[b] + c.

Python has special methods for inplace assignment (__setitem__) and inplace
arithmetic (__iadd__) but no special methods for inplace arithmetic and
assignment at the same time, so this is really out of NumPy's control here.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160808/cf81b142/attachment.html>


More information about the NumPy-Discussion mailing list