<p>在<em>逻辑<code>and</code>运算符中,如果第一个表达式是错误的,则不会计算第二个表达式</em>。由于<code>exists("")</code>失败,<code>os.path.getsize("")</code>永远不会在第一种情况下执行。在</p>
<p>但是在<em>位<code>&</code>运算符的情况下,两个操作数都必须求值</em>,才能得到结果。由于您不能<code>stat</code>一个无效的文件(以获得大小),它将失败并返回一个错误。在</p>
<p>引用<a href="https://docs.python.org/2/reference/expressions.html#boolean-operations" rel="nofollow">Boolean operations</a>文档</p>
<blockquote>
<p>The expression <code>x and y</code> first evaluates <code>x</code>; if <code>x</code> is false, its value is returned; otherwise, <code>y</code> is evaluated and the resulting value is returned.</p>
</blockquote>
<p>还要注意</p>
<blockquote>
<p>In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: <code>False</code>, <code>None</code>, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.</p>
</blockquote>
<p>所以,当你评估</p>
<pre><code>exists("") and os.path.getsize("")
</code></pre>
<p><code>exists("")</code>返回<code>False</code>,根据上面引用的文本,它将立即返回<code>False</code>,而不必执行<code>os.path.getsize("")</code>。在</p>