<p dir="auto"><strong>Suggestion</strong><br>
It would be very convenient if <code class="notranslate">np.asscalar()</code> would pass through scalars.  The use-case is to ensure that an argument is a scalar, regardless what is passed in (as long as it behaves as a scalar).</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.asarray(1.0)
1.0
&gt;&gt;&gt; np.asarray(1j)
1j"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">asarray</span>(<span class="pl-c1">1.0</span>)
<span class="pl-c1">1.0</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">asarray</span>(<span class="pl-c1">1j</span>)
<span class="pl-c1">1j</span></pre></div>
<p dir="auto">Presently these fail as the implementation simply looks for the <code class="notranslate">.item()</code> method, which scalars do not possess:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.asarray(1.0)
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-13-c6bc08be135e&gt; in &lt;module&gt;()
----&gt; 1 np.asscalar(1.0)

.../python2.7/site-packages/numpy/lib/type_check.pyc in asscalar(a)
    461 
    462     &quot;&quot;&quot;
--&gt; 463     return a.item()
    464 
    465 #-----------------------------------------------------------------------------

AttributeError: 'float' object has no attribute 'item'```"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">asarray</span>(<span class="pl-c1">1.0</span>)
<span class="pl-v">AttributeError</span>                            <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">13</span><span class="pl-c1">-</span><span class="pl-s1">c6bc08be135e</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>()
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-s1">np</span>.<span class="pl-en">asscalar</span>(<span class="pl-c1">1.0</span>)

...<span class="pl-c1">/</span><span class="pl-s1">python2</span>.<span class="pl-c1">7</span><span class="pl-c1">/</span><span class="pl-s1">site</span><span class="pl-c1">-</span><span class="pl-s1">packages</span><span class="pl-c1">/</span><span class="pl-s1">numpy</span><span class="pl-c1">/</span><span class="pl-s1">lib</span><span class="pl-c1">/</span><span class="pl-s1">type_check</span>.<span class="pl-s1">pyc</span> <span class="pl-c1">in</span> <span class="pl-en">asscalar</span>(<span class="pl-s1">a</span>)
    <span class="pl-c1">461</span> 
    <span class="pl-c1">462</span>     """
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">463</span>     <span class="pl-s1">return</span> <span class="pl-s1">a</span>.<span class="pl-en">item</span>()
    <span class="pl-c1">464</span> 
    <span class="pl-c1">465</span> <span class="pl-c">#-----------------------------------------------------------------------------</span>

<span class="pl-v">AttributeError</span>: <span class="pl-s">'float'</span> <span class="pl-s1">object</span> <span class="pl-s1">has</span> <span class="pl-s1">no</span> <span class="pl-s1">attribute</span> <span class="pl-s">'item'</span><span class="pl-s">``</span>`</pre></div>
<p dir="auto"><strong>Rational</strong><br>
It is common to use <code class="notranslate">np.asarray()</code> to ensure that the argument is an array (in the case that it might be a list etc.).  In an analogous use case, it would be useful to be able to use <code class="notranslate">np.asscalar()</code> to ensure that an argument is a scalar (as long as it can be converted to a scalar).</p>
<p dir="auto"><strong>Workaround</strong><br>
Convert the argument to an array first.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="np.asscalar(np.asarray(x))"><pre class="notranslate"><span class="pl-s1">np</span>.<span class="pl-en">asscalar</span>(<span class="pl-s1">np</span>.<span class="pl-en">asarray</span>(<span class="pl-s1">x</span>))</pre></div>
<p dir="auto"><strong>Resolution</strong><br>
Perhaps code like the following would work:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def asscalar(a):
    try:
        return a.item()
    except AttributeError, e:
        return np.asarray(a).item()"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">asscalar</span>(<span class="pl-s1">a</span>):
    <span class="pl-k">try</span>:
        <span class="pl-k">return</span> <span class="pl-s1">a</span>.<span class="pl-en">item</span>()
    <span class="pl-k">except</span> <span class="pl-v">AttributeError</span>, <span class="pl-s1">e</span>:
        <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">asarray</span>(<span class="pl-s1">a</span>).<span class="pl-en">item</span>()</pre></div>
<p dir="auto">np.random.randint results in ValueError when specifying arguments that overflow int32 on Windows. I've seen some related discussions around the platform dependent default int behavior (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="245755238" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9464" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/9464/hovercard" href="https://github.com/numpy/numpy/issues/9464">#9464</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="377272092" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/12332" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/12332/hovercard" href="https://github.com/numpy/numpy/issues/12332">#12332</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="634856919" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/16535" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/16535/hovercard" href="https://github.com/numpy/numpy/pull/16535">#16535</a>). If this classifies as a duplicate of the issues I've referenced, feel free to close this. (similar issue happens when creating an arrays of small integers which defaults to a dtype of int32, but you run into np.prod returning 0 if it overflows, but that's a separate discussion.)</p>
<p dir="auto">Right now code written on Linux won't work on Windows platforms (assuming both are 64 bit).</p>
<h3 dir="auto">Reproducing code example:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
np.random.randint(34353543525, 5465445754, size=5)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">34353543525</span>, <span class="pl-c1">5465445754</span>, <span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">5</span>)</pre></div>

<h3 dir="auto">Error message:</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-38-47b726322196&gt; in &lt;module&gt;
----&gt; 1 np.random.randint(34353543525, 5465445754, size=5)

mtrand.pyx in numpy.random.mtrand.RandomState.randint()

_bounded_integers.pyx in numpy.random._bounded_integers._rand_int32()

ValueError: high is out of bounds for int32"><pre class="notranslate"><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span>
<span class="pl-v">ValueError</span>                                <span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>)
<span class="pl-c1">&lt;</span><span class="pl-s1">ipython</span><span class="pl-c1">-</span><span class="pl-s1">input</span><span class="pl-c1">-</span><span class="pl-c1">38</span><span class="pl-c1">-</span><span class="pl-c1">47</span><span class="pl-s1">b726322196</span><span class="pl-c1">&gt;</span> <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
<span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-c1">1</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">34353543525</span>, <span class="pl-c1">5465445754</span>, <span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">5</span>)

<span class="pl-s1">mtrand</span>.<span class="pl-s1">pyx</span> <span class="pl-c1">in</span> <span class="pl-s1">numpy</span>.<span class="pl-s1">random</span>.<span class="pl-s1">mtrand</span>.<span class="pl-v">RandomState</span>.<span class="pl-en">randint</span>()

<span class="pl-s1">_bounded_integers</span>.<span class="pl-s1">pyx</span> <span class="pl-c1">in</span> <span class="pl-s1">numpy</span>.<span class="pl-s1">random</span>.<span class="pl-s1">_bounded_integers</span>.<span class="pl-en">_rand_int32</span>()

<span class="pl-v">ValueError</span>: <span class="pl-s1">high</span> <span class="pl-c1">is</span> <span class="pl-s1">out</span> <span class="pl-s1">of</span> <span class="pl-s1">bounds</span> <span class="pl-s1">for</span> <span class="pl-s1">int32</span></pre></div>
<h3 dir="auto">Numpy/Python version information:</h3>

<p dir="auto">1.18.1 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]</p>