【发布时间】:2014-05-25 05:45:13
【问题描述】:
我正在开发一个项目,其中包含多个模块。简化问题,有一些变量 x。有时它可能是 int、float 或 list。但它可能是一个 lambda 函数,应该以不同的方式处理。如何检查变量 x 是 lambda?
例如
>>> x = 3
>>> type(x)
<type 'int'>
>>> type(x) is int
True
>>> x = 3.4
>>> type(x)
<type 'float'>
>>> type(x) is float
True
>>> x = lambda d:d*d
>>> type(x)
<type 'function'>
>>> type(x) is lambda
File "<stdin>", line 1
type(x) is lambda
^
SyntaxError: invalid syntax
>>> type(x) is function
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>>
【问题讨论】: