diff options
author | mattip <matti.picus@gmail.com> | 2018-04-22 01:42:29 +0300 |
---|---|---|
committer | mattip <matti.picus@gmail.com> | 2018-04-23 22:31:42 +0300 |
commit | 86608c2883b9f1db468bf6d3b6d2a82a72f48b0f (patch) | |
tree | 6ab6c0fa10e4224bc024dcb2f44e753362c47460 /doc/source/reference | |
parent | f2888dbfc440cc3023b751fb7a5d91b9817fc271 (diff) | |
download | numpy-86608c2883b9f1db468bf6d3b6d2a82a72f48b0f.tar.gz |
DOC: cleanup documentation, continuation of nditer PR #9998
Diffstat (limited to 'doc/source/reference')
-rw-r--r-- | doc/source/reference/arrays.nditer.rst | 39 | ||||
-rw-r--r-- | doc/source/reference/c-api.iterator.rst | 16 |
2 files changed, 31 insertions, 24 deletions
diff --git a/doc/source/reference/arrays.nditer.rst b/doc/source/reference/arrays.nditer.rst index acad29b11..239f4296b 100644 --- a/doc/source/reference/arrays.nditer.rst +++ b/doc/source/reference/arrays.nditer.rst @@ -78,27 +78,28 @@ order='C' for C order and order='F' for Fortran order. ... 0 3 1 4 2 5 +.. _nditer-context-manager: + Modifying Array Values ---------------------- -By default, the :class:`nditer` treats the input array as a read-only -object. To modify the array elements, you must specify either read-write -or write-only mode. This is controlled with per-operand flags. The -operands may be created as views into the original data with the -`WRITEBACKIFCOPY` flag. In this case the iterator must either - -- be used as a context manager, and the temporary data will be written back - to the original array when the `__exit__` function is called. -- have a call to the iterator's `close` function to ensure the modified data - is written back to the original array. - -Regular assignment in Python simply changes a reference in the local or -global variable dictionary instead of modifying an existing variable in -place. This means that simply assigning to `x` will not place the value -into the element of the array, but rather switch `x` from being an array -element reference to being a reference to the value you assigned. To -actually modify the element of the array, `x` should be indexed with -the ellipsis. +By default, the :class:`nditer` treats the input operand as a read-only +object. To be able to modify the array elements, you must specify either +read-write or write-only mode using the `'readwrite'` or `'writeonly'` +per-operand flags. + +The nditer will then yield writeable buffer arrays which you may modify. However, +because the nditer must copy this buffer data back to the original array once +iteration is finished, you must signal when the iteration is ended, by one of two +methods. You may either: + + - used the nditer as a context manager using the `with` statement, and + the temporary data will be written back when the context is exited. + - call the iterator's `close` method once finished iterating, which will trigger + the write-back. + +The nditer can no longer be iterated once either `close` is called or its +context is exited. .. admonition:: Example @@ -186,7 +187,7 @@ construct in order to be more readable. 0 <(0, 0)> 1 <(0, 1)> 2 <(0, 2)> 3 <(1, 0)> 4 <(1, 1)> 5 <(1, 2)> >>> it = np.nditer(a, flags=['multi_index'], op_flags=['writeonly']) - >>> with it: + >>> with it: .... while not it.finished: ... it[0] = it.multi_index[1] - it.multi_index[0] ... it.iternext() diff --git a/doc/source/reference/c-api.iterator.rst b/doc/source/reference/c-api.iterator.rst index 314b62a16..6a9ec6005 100644 --- a/doc/source/reference/c-api.iterator.rst +++ b/doc/source/reference/c-api.iterator.rst @@ -709,6 +709,10 @@ Construction and Destruction the functions will pass back errors through it instead of setting a Python exception. + :c:func:`NpyIter_Deallocate` must be called for each copy. One call to + :c:func:`NpyIter_Close` is sufficient to trigger writeback resolution for + all copies since they share buffers. + .. c:function:: int NpyIter_RemoveAxis(NpyIter* iter, int axis)`` Removes an axis from iteration. This requires that @@ -761,8 +765,10 @@ Construction and Destruction .. c:function:: int NpyIter_Close(NpyIter* iter) - Resolves any needed writeback resolution. Must be called before - ``NpyIter_Deallocate``. After this call it is not safe to use the operands. + Resolves any needed writeback resolution. Should be called before + :c:func::`NpyIter_Deallocate`. After this call it is not safe to use the operands. + When using :c:func:`NpyIter_Copy`, only one call to :c:func:`NpyIter_Close` + is sufficient to resolve any writebacks, since the copies share buffers. Returns ``0`` or ``-1`` if unsuccessful. @@ -770,10 +776,10 @@ Construction and Destruction Deallocates the iterator object. - `NpyIter_Close` should be called before this. If not, and if writeback is - needed, it will be performed at this point in order to maintain + :c:func:`NpyIter_Close` should be called before this. If not, and if + writeback is needed, it will be performed at this point in order to maintain backward-compatibility with older code, and a deprecation warning will be - emmitted. Old code shuold be updated to call `NpyIter_Close` beforehand. + emmitted. Old code should be updated to call `NpyIter_Close` beforehand. Returns ``NPY_SUCCEED`` or ``NPY_FAIL``. |