You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>(a) When you omit the <codeclass="docutils literal notranslate"><spanclass="pre">copy</span></code> parameter, you should always immediately overwrite
305
-
the parameter array:</p>
304
+
<p>(a) When you omit the <codeclass="docutils literal notranslate"><spanclass="pre">copy</span></code> parameter, you should never reuse the parameter
305
+
array later on; ideally, you should reassign it immediately:</p>
<p>The anti-pattern below must be avoided, as it will result in different
311
-
behaviour on read-only versus writeable arrays:</p>
310
+
<p>The above best practice pattern ensures that the behaviour won’t change depending
311
+
on whether <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> is writeable or not, as the original <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> object is dereferenced
312
+
as soon as <codeclass="docutils literal notranslate"><spanclass="pre">xpx.at</span></code> returns; this way there is no risk to accidentally update it
313
+
twice.</p>
314
+
<p>On the reverse, the anti-pattern below must be avoided, as it will result in
315
+
different behaviour on read-only versus writeable arrays:</p>
<p>In the above example, <codeclass="docutils literal notranslate"><spanclass="pre">x</span><spanclass="pre">==</span><spanclass="pre">[0,</span><spanclass="pre">0,</span><spanclass="pre">0]</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">y</span><spanclass="pre">==</span><spanclass="pre">[2,</span><spanclass="pre">0,</span><spanclass="pre">0]</span></code> and z == <codeclass="docutils literal notranslate"><spanclass="pre">[0,</span><spanclass="pre">3,</span><spanclass="pre">0]</span></code>
318
-
when <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> is read-only, whereas <codeclass="docutils literal notranslate"><spanclass="pre">x</span><spanclass="pre">==</span><spanclass="pre">y</span><spanclass="pre">==</span><spanclass="pre">z</span><spanclass="pre">==</span><spanclass="pre">[2,</span><spanclass="pre">3,</span><spanclass="pre">0]</span></code> when <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> is
319
-
writeable!</p>
321
+
<p>In the above example, both calls to <codeclass="docutils literal notranslate"><spanclass="pre">xpx.at</span></code> update <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> in place <em>if possible</em>.
322
+
This causes the behaviour to diverge depending on whether <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> is writeable or not:</p>
323
+
<ulclass="simple">
324
+
<li><p>If <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> is writeable, then after the snippet above you’ll have
<spanclass="gp">>>> </span><spanclass="n">y</span><spanclass="o">=</span><spanclass="n">xpx</span><spanclass="o">.</span><spanclass="n">at</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">,</span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="o">.</span><spanclass="n">set</span><spanclass="p">(</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="n">copy</span><spanclass="o">=</span><spanclass="kc">True</span><spanclass="p">)</span><spanclass="c1"># Never updates x</span>
333
+
<spanclass="gp">>>> </span><spanclass="n">z</span><spanclass="o">=</span><spanclass="n">xpx</span><spanclass="o">.</span><spanclass="n">at</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">,</span><spanclass="mi">1</span><spanclass="p">)</span><spanclass="o">.</span><spanclass="n">set</span><spanclass="p">(</span><spanclass="mi">3</span><spanclass="p">)</span><spanclass="c1"># May or may not update x in place</span>
334
+
<spanclass="gp">>>> </span><spanclass="k">del</span><spanclass="n">x</span><spanclass="c1"># avoid accidental reuse of x as we don't know its state anymore</span>
335
+
</pre></div>
336
+
</div>
320
337
<p>(b) The array API standard does not support integer array indices.
321
338
The behaviour of update methods when the index is an array of integers is
322
339
undefined and will vary between backends; this is particularly true when the
0 commit comments