<p>内置命名空间中没有构造函数的内置类型(如函数、生成器、方法)位于<code>types</code>模块中。您可以在<code>isinstance</code>调用中使用<code>types.FunctionType</code></p>
<pre><code>In [1]: import types
In [2]: types.FunctionType
Out[2]: <type 'function'>
In [3]: def f(): pass
...:
In [4]: isinstance(f, types.FunctionType)
Out[4]: True
In [5]: isinstance(lambda x : None, types.FunctionType)
Out[5]: True
</code></pre>
<p>请注意,这使用了一个非常具体的“函数”概念,这通常不是您所需要的。例如,它拒绝<code>zip</code>(技术上是一个类):</p>
<pre><code>>>> type(zip), isinstance(zip, types.FunctionType)
(<class 'type'>, False)
</code></pre>
<p><code>open</code>(内置函数的类型不同):</p>
<pre><code>>>> type(open), isinstance(open, types.FunctionType)
(<class 'builtin_function_or_method'>, False)
</code></pre>
<p>以及<code>random.shuffle</code>(技术上是隐藏的<code>random.Random</code>实例的方法):</p>
<pre><code>>>> type(random.shuffle), isinstance(random.shuffle, types.FunctionType)
(<class 'method'>, False)
</code></pre>
<p>如果您正在执行特定于<code>types.FunctionType</code>实例的操作,例如反编译它们的字节码或检查闭包变量,请使用<code>types.FunctionType</code>,但是如果您只需要像函数一样可以调用对象,请使用<code>callable</code>。</p>