python中的None與NULL用法說明
None是一個對象,而NULL是一個類型。
Python中沒有NULL,只有None,None有自己的特殊類型NoneType。
None不等于0、任何空字符串、False等。
在Python中,None、False、0、''(空字符串)、[](空列表)、()(空元組)、{}(空字典)都相當于False。
判斷變量是否為空的高效方法是:if X is None
if not X:當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測試結果’ print x is None #False print y is None #Trueprint ’not X測試結果’ print not x #True print not y #Trueprint ’not X is None測試結果’ print not x is None #True print not y is None #Falseprint ’X is not None測試結果’ print x is not None #True print y is not None #False
補充:python中None與0、Null、false區別
None是Python中的一個關鍵字,None本身也是個一個數據類型,而這個數據類型就是None,它可0、空字符串以及false均不一樣,這些都只是對象,而None也是一個類。
給個bool測試:val = Noneif val: print 'None is true'else: print 'None is not true'#輸出None is not true
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
