擅长:python、mysql、java
<p>从<a href="https://docs.python.org/3/reference/executionmodel.html?#resolution-of-names" rel="nofollow noreferrer">Resolution of Names</a>:</p>
<blockquote>
<p><strong>If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block.</strong> This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. <strong>The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.</strong></p>
</blockquote>
<p>(我的)</p>
<p>赋值是<a href="https://docs.python.org/3/reference/executionmodel.html?#binding-of-names" rel="nofollow noreferrer">name binding operations</a>,因此只要<code>count = 0</code>存在于函数中<em>的任何地方,<code>count</code>被认为是该函数的局部变量,<code>inner</code>将引用该变量。(不过,如果在将值赋给<code>count</code>之前调用</em><code>inner()</code>,则会得到一个异常。)</p>