python - isTrue 函数有什么用吗?

标签 python function

def istrue(a):
    if a:
        return True
    return False

这样的功能能以某种方式占有一席之地吗? 我的意思是,如果我想检查一个变量的真实性,我总是可以使用

if variable:
   do that

提前致谢

最佳答案

不,定义一个这样的函数:

def istrue(a):
    if a:
        return True
    else:
        return False

并像这样使用它:

value = True
if istrue(value):
     print('True!')

与此没有什么不同:

if bool(value):
    print('True!')

或者这个:

if value:
    print('True!')

因此,将 bool 值抽象为一个函数不会有任何作用。但是,可能有一个用例来确定 a 是否是 bool 值的实例以及对 a 进行 bool 值评估以确定它是否是文字 。但即便如此我也不会抽象成一个函数,我会这样写:

if value is True:
    print('True!')

关于python - isTrue 函数有什么用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174366/

相关文章:

c - 在 C 中将指针作为函数参数传递

c 函数没有被调用?

python - 在 Python 中将曲线拟合到数据集

python - 返回 Python 中最低有效位的索引

c++ - 为什么 std::function 不能相等比较?

r - 使用For循环对不同的链接进行层次聚类

c - 函数无法识别 typedef 参数

Python:打印/获取每个段落的第一句

python - 使用子图和循环按组绘制 Pandas

python - 在 OS X 上使用 Python 安装 OpenCV 时出错