1. if x is not None
2,if not x
3 if not x is None 這里的x等于None,False,空字串,0,空串列[],空字典{},空元組()時對你的判斷沒有影響。
即:
not None == not False. == not ‘‘ == not 0 == not []== not {} == not ()
if x is not None ;和 if not x is None;是一樣的,一只是不同的寫法,就是判斷 x是不是等于None的情況,只有x不是None的時候才會執行冒號后的陳述句。
代碼如下;
x=[]
y=None
x is None
》False
y is None
》True
x is notNone
》True
y is not None
》False
可以看出not x和not y都為True,not []和not None是等價的,無法區分彼此。
推薦使用if x is not None;谷歌的寫法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/113816.html
