Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
考虑这段代码:
test_string = 'not empty' if test_string: return True else: return False
我知道我可以构造一个条件表达式来做到这一点:
return True if test_string else False
但是,当我宁愿只返回布尔值时,我不喜欢测试布尔值是真还是假。我将如何返回它的真实性?
您可以使用bool:
bool
return bool(test_string)
演示:
>>> bool('abc') True >>> bool('') False >>>