我正在尝试创建一个 lambda 函数,它将对传递给它的值进行四舍五入,并指定小数位数
我做了以下内容
c = lambda x: round(x, dp)
其中 dp 已指定
但是,尝试调用它会出现以下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: 'int' object is not callable
当我打字时
c(23.2323332345435)
我可能半梦半醒,但我很确定这就是创建一个简单的 lambda 函数的方式,而且我不明白为什么它不起作用。
我尝试用括号括起来,但没有成功
最佳答案
如果您在定义 lambda 之前或之后意外重写了 round
,就会发生这种情况:
>>> c = lambda x: round(x, 5)
>>> c(34.44)
34.44
>>> round=12
>>> c(34.44)
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 1, in <lambda>
TypeError: 'int' object is not callable
>>>
关于python - 舍入 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139968/