擅长:python、mysql、java
<p><a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">official PEP 8 recommendation</a>是通过身份检查来测试<em>None</em>:</p>
<pre><code>if Variable is not None:
print "Hello world"
</code></pre>
<p>平等/不平等测试也会起作用,但速度会慢一些,而且不是惯用的方法:</p>
<pre><code>if Variable != None:
print "Hello world"
</code></pre>
<p>测试<em>变量</em>的布尔值会产生不同的结果。如果变量是空容器或等于零的数字,则以下测试将打印“hello world”:</p>
<pre><code># Hello world won't print for any of these values:
for Variable in (), '', u'', {}, [], 0, 0.0, 0j, None:
if Variable:
print "Hello world"
</code></pre>