python not x 和 x is None 的应用

python not x 和 x is None 的应用,第1张

python not x 和 x is None 的应用 aa = []

bb = [1] bc

print(not aa, not b) # True False d

cc = [] d

dd = None ccde

f

print(cc is None, dd is None) # False True (可以看到在判空列表时这样写是容易出错的)

print(not cc, not dd) # True True

ee = True eef

ff = False f

print(ee is None, ff is None) # Falshe False

print(not ee, not ff) # False True 

hh = ()

print(hh is None, not hh) # False True

总结:

1、判断列表建议用not x,用x is None 会有问题!

2、判断None对象,则 not x 与 x is None是一样的效果!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5480775.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存