<p>Google和<a href="http://www.python.org/dev/peps/pep-0008/#programming-recommendations" rel="noreferrer">Python</a>的风格指南都是最佳实践:</p>
<pre><code>if x is not None:
# Do something about x
</code></pre>
<p>使用<code>not x</code>可能会导致不需要的结果。见下文:</p>
<pre><code>>>> x = 1
>>> not x
False
>>> x = [1]
>>> not x
False
>>> x = 0
>>> not x
True
>>> x = [0] # You don't want to fall in this one.
>>> not x
False
</code></pre>
<p>您可能有兴趣查看在Python中计算为<code>True</code>或<code>False</code>的文本:</p>
<ul>
<li><a href="http://docs.python.org/library/stdtypes.html" rel="noreferrer">Truth Value Testing</a></li>
</ul>
<p><strong>在下面编辑评论:</strong></p>
<p>我只是做了更多的测试。<code>not x is None</code>不会先否定<code>x</code>,然后再与<code>None</code>进行比较。实际上,这样使用时,<code>is</code>运算符的优先级似乎更高:</p>
<pre><code>>>> x
[0]
>>> not x is None
True
>>> not (x is None)
True
>>> (not x) is None
False
</code></pre>
<p>因此,<code>not x is None</code>在我看来,最好避免。</p>
<p><strong>更多编辑:</strong></p>
<p>我只是做了更多的测试,可以确认bukzor的评论是正确的。(至少,我无法证明这一点。)</p>
<p>这意味着<code>if x is not None</code>的结果与<code>if not x is None</code>的结果完全相同。我坚持改正。谢谢布克佐尔。</p>
<p>然而,我的答案仍然是:<strong>使用传统的<code>if x is not None</code></strong>。<code>:]</code></p>