gpt4 book ai didi

python - 简化if语句python

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:52 25 4
gpt4 key购买 nike

是否有任何 Pythonic 或紧凑的方法来编写以下 if 语句:

if head is None and tail is None:
print("Test")

类似于:

if (head and tail) is None: 

最佳答案

如果 headtail 都是没有长度或 bool 值的自定义类实例(如 Node() 或类似的),那么只需使用:

if not (head or tail):

如果以太 headtail 可能是除 None 之外的对象且具有 false-y 值( False、数字 0、空容器等)。

否则,您将陷入显式测试。 bool 逻辑没有“英语语法”捷径。

关于python - 简化if语句python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40933576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com