<div dir="ltr"><div class="markdown-here-wrapper" style=""><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;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):
    x = np.asanyarray(x) # convert scalars to 0d arrays

    # The magic happens here

    return ret[()]  # convert 0d arrays to scalars
</code></pre><p style="margin:0px 0px 1.2em!important">Eric</p>
<p style="margin:0px 0px 1.2em!important">On Tue, 12 Dec 2017 at 11:58 Robert Kern <<a href="mailto:robert.kern@gmail.com" target="_blank">robert.kern@gmail.com</a>> wrote:</p>
<p style="margin:0px 0px 1.2em!important"></p><div class="markdown-here-exclude"><p></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">On Wed, Dec 13, 2017 at 4:50 AM, Joe <<a href="mailto:solarjoe@posteo.org" target="_blank">solarjoe@posteo.org</a>> wrote:<br>><br>> Hi,<br>><br>> the best example I found was this one:<br>><br>> <a href="https://stackoverflow.com/a/29319864/7919597" target="_blank">https://stackoverflow.com/a/29319864/7919597</a><br>><br>> def func_for_scalars_or_vectors(x):<br>>     x = np.asarray(x)<br>>     scalar_input = False<br>>     if x.ndim == 0:<br>>         x = x[None]  # Makes x 1D<br>>         scalar_input = True<br>><br>>     # The magic happens here<br>><br>>     if scalar_input:<br>>         return np.squeeze(ret)<br>>     return ret<br>><br>> Is this as good as it gets or do you have other suggestions?<br><br></div><div dir="ltr">In general structure, yeah. I'd probably use `np.isscalar(x)` for the test and `x = np.atleast_1d(x)` for the coercion for readability, but otherwise, that's it.<br><br>--<br>Robert Kern</div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote><p></p></div><p style="margin:0px 0px 1.2em!important"></p>
<div title="MDH:VXNpbmcgYG5wLmlzY2FsYXJgIGlzIGEgYmFkIGlkZWEsIGFzIGl0IGZhaWxzIGZvciAwZCBhcnJh
eXMuIGB4Lm5kaW1gIGlzIHRoZSBiZXR0ZXIgb3B0aW9uIHRoZXJlLjxkaXY+PGJyPjwvZGl2Pjxk
aXY+SSdkIG1heWJlIHN1Z2dlc3Qgbm90IHNwZWNpYWwtY2FzaW5nIDBkIGFycmF5cyB0aG91Z2gs
IGFuZCB1c2luZzo8L2Rpdj48ZGl2Pjxicj48L2Rpdj48ZGl2PmBgYDwvZGl2PjxkaXY+ZGVmIGZ1
bmNfZm9yX3NjYWxhcnNfb3JfdmVjdG9ycyh4KTx3YnI+OjwvZGl2PjxkaXY+Jm5ic3A7ICZuYnNw
OyB4ID0gbnAuYXNhbnlhcnJheSh4KSAjIGNvbnZlcnQgc2NhbGFycyB0byAwZCBhcnJheXM8L2Rp
dj48ZGl2Pjxicj48L2Rpdj48ZGl2IGRpcj0ibHRyIj48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ZRakNOSEZCQmQ4R3F6UFM4c243NWwwdEp0Yno0UGlXUSI+aHR0cHM6Ly9t
YWlsLnB5dGhvbi5vcmcvPHdicj5tYWlsbWFuL2xpc3RpbmZvL251bXB5LTx3YnI+ZGlzY3Vzc2lv
bjwvYT48YnI+CjwvYmxvY2txdW90ZT48L2Rpdj4=" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0">​</div></div></div>