擅长:python、mysql、java
<p>全局变量是特殊的。如果试图分配给函数内部的变量<code>a = value</code>,它会在函数内部创建一个新的局部变量,即使存在同名的全局变量。要访问全局变量,请在函数中添加<a href="http://docs.python.org/reference/simple_stmts.html#the-global-statement" rel="noreferrer">^{<cd2>} statement</a>:</p>
<pre><code>a = 7
def setA(value):
global a # declare a to be a global
a = value # this sets the global value of a
</code></pre>
<p>有关Python命名和绑定规则的详细说明,请参见<a href="http://docs.python.org/reference/executionmodel.html#naming-and-binding" rel="noreferrer">Naming and binding</a>。</p>