kra*_*r65 2 python types function
我有一个列表,其中一些也可以是函数.如果它是一个函数我想执行它.为此我做了类型检查.这通常适用于其他类型,如str,int或float.但是对于一个功能它似乎不起作用:
>>> def f():
... pass
...
>>> type(f)
<type 'function'>
>>> if type(f) == function: print 'It is a function!!'
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>>
Run Code Online (Sandbox Code Playgroud)
有人知道如何检查功能类型吗?
小智 5
import types
if type(f) == types.FunctionType:
print 'It is a function!!'
Run Code Online (Sandbox Code Playgroud)