您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

“如果x”与“如果x不为无”之间的区别

“如果x”与“如果x不为无”之间的区别

在以下情况下:

test = False 
test = "" 
test = 0
test = 0.0 
test = []
test = () 
test = {} 
test = set()

if测试将有所不同:

if test: #False

if test is not None: #True

之所以如此,是因为is测试身份,含义

test is not None

相当于

id(test) == id(None) #False

因此

(test is not None) is (id(test) != id(None)) #True
其他 2022/1/1 18:47:53 有487人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶