<p dir="auto">I have not thought it out, but there was a (now cleared out) issue about <code class="notranslate">np.nditer</code> not allowing to access the <code class="notranslate">it.operands</code> after the <code class="notranslate">with</code> statement is finished. For return operands, accessing it only after finishing everything doesn't seem unreasonable though.</p>
<p dir="auto">EDIT: Example code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np

def foo(a):
    it = np.nditer([a, None])
    with it:
        for x,y in it:
            y[...] = x * x
    return it.operands[1]
b = np.arange(2)
print(foo(b))"><pre class="notranslate"><code class="notranslate">import numpy as np

def foo(a):
    it = np.nditer([a, None])
    with it:
        for x,y in it:
            y[...] = x * x
    return it.operands[1]
b = np.arange(2)
print(foo(b))
</code></pre></div>
<p dir="auto">is not currently valid.</p>
<p dir="auto">See some discussion in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="316534906" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/10951" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/10951/hovercard" href="https://github.com/numpy/numpy/pull/10951">#10951</a>.</p>
<p dir="auto">The issue is that the nditer <code class="notranslate">operands</code> attribute does not always refer to the actual operand array, but rather to the temporary buffer/copy of the operand array, despite what the nditer docstring says.</p>
<p dir="auto">This is a problem for code like:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; def square(a):
...     with np.nditer([a, None]) as it:
...         for x, y in it:
...             y[...] = x*x
...         return it.operands[1]"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">def</span> <span class="pl-en">square</span>(<span class="pl-s1">a</span>):
...     <span class="pl-k">with</span> <span class="pl-s1">np</span>.<span class="pl-en">nditer</span>([<span class="pl-s1">a</span>, <span class="pl-c1">None</span>]) <span class="pl-k">as</span> <span class="pl-s1">it</span>:
...         <span class="pl-k">for</span> <span class="pl-s1">x</span>, <span class="pl-s1">y</span> <span class="pl-c1">in</span> <span class="pl-s1">it</span>:
...             <span class="pl-s1">y</span>[...] <span class="pl-c1">=</span> <span class="pl-s1">x</span><span class="pl-c1">*</span><span class="pl-s1">x</span>
...         <span class="pl-k">return</span> <span class="pl-s1">it</span>.<span class="pl-s1">operands</span>[<span class="pl-c1">1</span>]</pre></div>
<p dir="auto">since the returned value could in principle be a temporary buffer, rather than the newly created 'out' array. In practice, this hasn't been a problem because the out arrays are never buffered, I think.</p>
<p dir="auto">Because of our recent improvements to the nditer as a context manager, and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="316534906" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/10951" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/10951/hovercard" href="https://github.com/numpy/numpy/pull/10951">#10951</a>, there is a second problem that the operands cannot be accessed once the iterator context is exited. For instance, this means the <code class="notranslate">return</code> statement above can't currently be placed outside the with statement, even though in principle that is where it should go because the operand is only "written back to" at context exit.</p>