How to determine number of function/builtin/callable parameters programmatically?
<p>I want to be able to classify an unknown function/builtin/callable based on its number of arguments.</p>

<p>I may write a piece of code like that:</p>

<pre><code>if numberOfParameters(f) == 0:
    noParameters.append(f)
elif numberOfParameters(f) == 1:
    oneParameter.append(f)
else:
    manyParameters.append(f)
</code></pre>

<p>but I have no idea how to implement <code>numberOfParameters()</code>.  <code>inspect.getargspec</code> does not work with builtins. I can not use exceptions, since it is potentially expensive to call the function.</p>

<p>It would be great if the solution work with both Python 2.7 and Python 3.3+</p>

<p>From Python 3.3</p>

<blockquote>
  <p>New in version 3.3.</p>
</blockquote>

<p><a href="https://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object" rel="nofollow"><strong>Introspecting callables with the Signature object</strong></a></p>

<pre><code>&gt;&gt;&gt; import inspect
&gt;&gt;&gt; inspect.signature(sorted).parameters
mappingproxy(OrderedDict([('iterable', &lt;Parameter "iterable"&gt;), ('key', &lt;Parameter "key=None"&gt;), ('reverse', &lt;Parameter "reverse=False"&gt;)]))
</code></pre>

<p>And then you can count them, and do whatever you like.</p>

python python-2.7 function python-3.x callable