python中的None與NULL用法說明
None是一個對象,而NULL是一個類型。
Python中沒有NULL,只有None,None有自己的特殊類型NoneType。
None不等于0、任何空字符串、False等。
在Python中,None、False、0、''(空字符串)、[](空列表)、()(空元組)、{}(空字典)都相當(dāng)于False。
判斷變量是否為空的高效方法是:if X is None
if not X:當(dāng)X為None、False、''、0、[]、()、{}時,not X為真,無法分辨
if not X is None:等價于if not (X is None)、if X is not None
判斷空使用指南if X is not None寫法清晰明了,且不會出錯,推薦使用;
if not x使用前,必須確定X為None、False、''、0、[]、()、{}時對判斷無影響。
示例x = [] y = Noneprint ’X is None測試結(jié)果’ print x is None #False print y is None #Trueprint ’not X測試結(jié)果’ print not x #True print not y #Trueprint ’not X is None測試結(jié)果’ print not x is None #True print not y is None #Falseprint ’X is not None測試結(jié)果’ print x is not None #True print y is not None #False
補(bǔ)充:python中None與0、Null、false區(qū)別
None是Python中的一個關(guān)鍵字,None本身也是個一個數(shù)據(jù)類型,而這個數(shù)據(jù)類型就是None,它可0、空字符串以及false均不一樣,這些都只是對象,而None也是一個類。
給個bool測試:val = Noneif val: print 'None is true'else: print 'None is not true'#輸出None is not true
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. chat.asp聊天程序的編寫方法3. CSS 使用Sprites技術(shù)實現(xiàn)圓角效果4. phpstudy apache開啟ssi使用詳解5. 詳解瀏覽器的緩存機(jī)制6. ASP中if語句、select 、while循環(huán)的使用方法7. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?8. HTML中的XML數(shù)據(jù)島記錄編輯與添加9. 利用FastReport傳遞圖片參數(shù)在報表上展示簽名信息的實現(xiàn)方法10. 推薦一個好看Table表格的css樣式代碼詳解
