python中的None與NULL用法說(shuō)明
None是一個(gè)對(duì)象,而NULL是一個(gè)類型。
Python中沒(méi)有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、[]、()、{}時(shí),not X為真,無(wú)法分辨
if not X is None:等價(jià)于if not (X is None)、if X is not None
判斷空使用指南
if X is not None寫法清晰明了,且不會(huì)出錯(cuò),推薦使用;
if not x使用前,必須確定X為None、False、""、0、[]、()、{}時(shí)對(duì)判斷無(wú)影響。
示例
x = [] y = None print 'X is None測(cè)試結(jié)果' print x is None #False print y is None #True print 'not X測(cè)試結(jié)果' print not x #True print not y #True print 'not X is None測(cè)試結(jié)果' print not x is None #True print not y is None #False print 'X is not None測(cè)試結(jié)果' print x is not None #True print y is not None #False
補(bǔ)充:python中None與0、Null、false區(qū)別
None是Python中的一個(gè)關(guān)鍵字,None本身也是個(gè)一個(gè)數(shù)據(jù)類型,而這個(gè)數(shù)據(jù)類型就是None,它可0、空字符串以及false均不一樣,這些都只是對(duì)象,而None也是一個(gè)類。
給個(gè)bool測(cè)試:
val = None if val: print "None is true" else: print "None is not true" #輸出 None is not true
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。