作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想将一个数学运算符以及要比较的数值传递给一个函数。这是我损坏的代码:
def get_truth(inp,relate,cut):
if inp print(relate) cut:
return True
else:
return False
并调用它
get_truth(1.0,'>',0.0)
应该返回 True。
最佳答案
看看operator module :
import operator
get_truth(1.0, operator.gt, 0.0)
...
def get_truth(inp, relate, cut):
return relate(inp, cut)
# you don't actually need an if statement here
关于python - 如何将运算符传递给python函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591778/
我是一名优秀的程序员,十分优秀!