python中if not x: 和 if x is not None: 和 if not x is None的使用和区别
代码中经常会有变量是否为None的判断,有三种主要的写法
第一种是'if x is None';
第二种是 'if not x:';
第三种是'if not x is None'(这句这样理解更清晰'if not (x is None)') 。
如果你觉得这样写没啥区别,那么你可就要小心了,这里面有一个坑。先来看一下代码:
第一种是'if x is None';
第二种是 'if not x:';
第三种是'if not x is None'(这句这样理解更清晰'if not (x is None)') 。
如果你觉得这样写没啥区别,那么你可就要小心了,这里面有一个坑。先来看一下代码:
>>> x = 1 >>> not x False >>> x = [1] >>> not x False >>> x = 0 >>> not x True >>> x = [0] # You don't want to fall in this one. >>> not x False
在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False ,即:
因此在使用列表的时候,如果你想区分x==[]和x==None两种情况的话, 此时'if not x:'将会出现问题:
>>> x = [] >>> y = None >>> >>> x is None False >>> y is None True >>> >>> >>> not x True >>> not y True >>> >>> >>> not x is None >>> True >>> not y is None False >>>
也许你是想判断x是否为None,但是却把'x==[]'的情况也判断进来了,此种情况下将无法区分。
对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。
而对于'if x is not None'和'if not x is None'写法,很明显前者更清晰,而后者有可能使读者误解为'if (not x) is None',因此推荐前者,同时这也是谷歌推荐的风格
结论:
'if x is not None'是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。
使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通