三種主要的寫法有:
第一種:if X is None;
第二種:if not X;
當X為None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元組()這些時,not X為真,即無法分辨出他們之間的不同。
第三種:if not X is None;
在Python中,None、空列表[]、空字典{}、空元組()、0等一系列代表空和無的對象會被轉換成False。除此之外的其它對象都會被轉化成True。
在命令if not 1中,1便會轉換為bool類型的True。not是邏輯運算符非,not 1則恆為False。因此if語句if not 1之下的語句,永遠不會執行。
========================================================================================
對比:foo is None 和 foo == None
示例:
>>> class Foo(object): def __eq__(self, other): return True >>> f = Foo() >>> f == None True >>> f is None False
=============================================
python中的not具體表示是什么,舉個例子說一下,衷心的感謝
在python中not是邏輯判斷詞,用於布爾型True和False,not True為False,not False為True,以下是幾個常用的not的用法: (1) not與邏輯判斷句if連用,代表not后面的表達式為False的時候,執行冒號后面的語句。比如: a = False if not a: (這里因為a是False,所以not a就是True) print "hello" 這里就能夠輸出結果hello (2) 判斷元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,這句話的意思是如果a不在列表b中,那么就執行冒號后面的語句,比如: a = 5 b = [1, 2, 3] if a not in b: print "hello" 這里也能夠輸出結果hello
not x 意思相當於 if x is false, then True, else False
=====================================================================================================
感謝
參考來源:http://blog.csdn.net/sasoritattoo/article/details/12451359
http://stackoverflow.com/questions/26595/is-there-any-difference-between-foo-is-none-and-foo-none
http://stackoverflow.com/questions/2710940/python-if-x-is-not-none-or-if-not-x-is-none