return not用法
>>> a=[]>>> def print():
return not a
>>> print()
True
问题:可以帮忙解释一下return not 的意思和用法吗?它只能返回 Ture 和 False 吗?
本帖最后由 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 在?推荐用 if x is not None 比 if not x is None要好
页:
[1]