dandan0523 发表于 2021-4-10 09:02:01

return not用法

>>> a=[]
>>> def print():
        return not a

>>> print()
True


问题:可以帮忙解释一下return not 的意思和用法吗?它只能返回 Ture 和 False 吗?

dandan0523 发表于 2021-4-10 09:13:32

本帖最后由 dandan0523 于 2021-4-10 09:24 编辑

抱歉各位,问题已解决
代码中经常会有变量是否为None的判断,有三种主要的写法:

第一种是`if x is None`;

第二种是 `if not x:`;

第三种是`if not x is None`(这句这样理解更清晰`if not (x is None)`)
在python中 None,False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False

kogawananari 发表于 2021-4-10 12:52:51

在?推荐用 if x is not None 比 if not x is None要好
页: [1]
查看完整版本: return not用法