網(wǎng)站首頁 編程語言 正文
if xxx 和if xxx is None的區(qū)別
一、 if xxx
None,’’,0,[],{},() ,False都被判斷為空值(not xxx等價(jià))
如下代碼輸出所示,
if __name__ == '__main__':
? ? print("---not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False)---")
? ? print(not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False))
輸出
---not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False)---
True
if xxx
如下代碼輸出所示,
if __name__ == '__main__':
? ? print("---output a,b---")
? ? a = []
? ? b = None
? ? print("a=[]")
? ? print("b=None")
? ? print("--- if x")
? ? if a:
? ? ? ? print("a")
? ? else:
? ? ? ? print("None")
? ? if b:
? ? ? ? print("b")
? ? else:
? ? ? ? print("None")
輸出
---output a,b---
a=[]
b=None
--- if x
None
None
結(jié)論:
將空列表換成上述的其他空類型,結(jié)果一樣。
如果需要過濾None值和空對(duì)象時(shí)(如[],{},''等),可使用這種寫法
二、 if xxx is None
該寫法可將None和其他空值對(duì)象區(qū)分開來
如下代碼輸出所示:
if __name__ == '__main__':
? ? a = []
? ? b = None
? ? print("a=[]")
? ? print("b=None")
? ? print("--- is None")
? ? if a is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("a")
? ? if b is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("b")
輸出
---output a,b---
a=[]
b=None
--- is None
a
None
結(jié)論:
需要區(qū)分[],{},'',()等空值對(duì)象與None的區(qū)別時(shí)時(shí)可使用這種寫法
貼下簡單的測試代碼
if __name__ == '__main__':
? ? print("---not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False)---")
? ? print(not None == (not '') == (not 0) == (not []) == (not {}) == (not ()) == (not False))
? ? print("---output a,b---")
? ? a = []
? ? b = None
? ? print("a=[]")
? ? print("b=None")
? ? print("--- if x")
? ? if a:
? ? ? ? print("a")
? ? else:
? ? ? ? print("None")
? ? if b:
? ? ? ? print("b")
? ? else:
? ? ? ? print("None")
? ? print("--- is None")
? ? if a is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("a")
? ? if b is None:
? ? ? ? print("None")
? ? else:
? ? ? ? print("b")
? ? print("--- not")
? ? if not a:
? ? ? ? print("None")
? ? else:
? ? ? ? print("a")
? ? if not b:
? ? ? ? print("None")
? ? else:
? ? ? ? print("b")
? ? print("--- is not None")
? ? if a is not None:
? ? ? ? print("a")
? ? else:
? ? ? ? print("None")
? ? if b is not None:
? ? ? ? print("B")
? ? else:
? ? ? ? print("None")
原文鏈接:https://blog.csdn.net/Urms_handsomeyu/article/details/103350227
相關(guān)推薦
- 2022-11-17 Rust中的Cargo構(gòu)建、運(yùn)行、調(diào)試_相關(guān)技巧
- 2022-12-04 .NET?Core利用BsonDocumentProjectionDefinition和Lookup
- 2022-07-06 Android中ViewFlipper和AdapterViewFlipper使用的方法實(shí)例_Andr
- 2023-03-01 Shell?$[]對(duì)整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算實(shí)現(xiàn)_linux shell
- 2023-02-26 詳解Golang時(shí)間處理的踩坑及解決_Golang
- 2022-12-07 C++?Boost?Container庫示例詳細(xì)講解_C 語言
- 2022-09-05 Hbase之查看RowKey所在Region
- 2022-03-03 uniapp的報(bào)錯(cuò)ncaught Error: Module build failed (from
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支