【发布时间】:2013-07-10 20:45:27
【问题描述】:
我有一个东西列表,其中一些也可以是函数。如果它是一个函数,我想执行它。为此,我进行了类型检查。这通常适用于其他类型,例如 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
>>>
有人知道如何检查函数类型吗?
【问题讨论】:
-
你也可以使用 try/except 代替检查类型。