美文网首页
判断是否为None

判断是否为None

作者: GRUNMI | 来源:发表于2019-10-21 14:40 被阅读0次
判断是否为None的情况

if not x

if x is None

if not x is None

if x is not None是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。

使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行

在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False,即:
not None == not False == not '' == not 0 == not [] == not {} == not ()

相关文章

网友评论

      本文标题:判断是否为None

      本文链接:https://www.haomeiwen.com/subject/touvpftx.html