這個其實在Python文檔當中有寫了,爲了準確起見,咱們先引用Python文檔當中的原文:html
In the context of Boolean operations, and also when expressions are used bycontrol flow statements, the following values are interpreted as false:False, None, numeric zero of all types, and empty strings and containers(including strings, tuples, lists, dictionaries, sets and frozensets). Allother values are interpreted as true. (See the __nonzero__()special method for a way to change this.)
進行邏輯判斷(好比if)時,Python當中等於False的值並不僅有False一個,它也有一套規則。對於基本類型來講,基本上每一個類型都存在一個值會被斷定爲False。大體是這樣:python
自定義類型則服從下面的規則:express
因此回到問題,not None的確會返回True。不過必定要警戒的是,if a is None和if a,if a is not None和if not a不能夠隨便混用,前面也說過了,它們在不少時候是不一樣的,好比說當a是一個列表的時候if not a實際上是判斷a爲None或者爲空。this