<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Dec 13, 2017 at 5:52 AM, Eric Wieser <span dir="ltr"><<a href="mailto:wieser.eric+numpy@gmail.com" target="_blank">wieser.eric+numpy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="m_2576370390024422606markdown-here-wrapper"><p style="margin:0px 0px 1.2em!important">Using <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.iscalar</code> is a bad idea, as it fails for 0d arrays. <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">x.ndim</code> is the better option there.</p>
<p style="margin:0px 0px 1.2em!important">I’d maybe suggest not special-casing 0d arrays though, and using:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important">def func_for_scalars_or_vectors(x)<wbr>:
    x = np.asanyarray(x) # convert scalars to 0d arrays

    # The magic happens here

    return ret[()]  # convert 0d arrays to scalars</code></pre></div></div></blockquote></div><div>Good call. I didn't think that the empty tuple was valid for anything but indexing into 0d arrays, but of course, following the rules of indexing, it works as required for other arrays too.</div><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Robert Kern</div>
</div></div>