擅长:python、mysql、java
<p>让我们问一下python shell。。。你知道吗</p>
<pre><code>In [1]: None == 0
Out[1]: False
In [2]: not None
Out[2]: True
In [3]: None?
Type: NoneType
String form: None
Namespace: Python builtin
Docstring: <no docstring>
</code></pre>
<p>所以这可能会给你一个提示,为什么<code>None==0</code>返回False。进一步提示。。你知道吗</p>
<pre><code>In [6]: i = 0
In [7]: i?
Type: int
String form: 0
Docstring:
int(x=0) -> int or long
int(x, base=10) -> int or long
</code></pre>
<p>有了这些信息,我们就可以深入了解python的真相系统。。。你知道吗</p>
<blockquote>
<p>The following values are considered false:</p>
<p>None</p>
<p>False</p>
<p>zero of any numeric type, for example, 0, 0L, 0.0, 0j.</p>
</blockquote>
<p>是的,对于<code>if</code>/<code>while</code>条件,<code>None</code>和<code>0</code>都被认为是假的,但这并不意味着所有计算为“假”的事物本身都是相等的。由于它们中的一些有不同的类型,它们不是等价的。你知道吗</p>
<p>所以底线是:</p>
<p>[1]在<code>if</code>/<code>while</code>语句中,并非所有计算结果都是<code>False</code>的语句本身是相等的。你知道吗</p>
<p>[2]原因是Python的类型系统</p>