You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
4.1 KiB
29 lines
4.1 KiB
<div class="section" id="the-global-statement">
|
|
<span id="global"></span><h2>The <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement</h2>
|
|
<pre id="index-39">
|
|
<strong id="grammar-token-global_stmt">global_stmt</strong> ::= "global" <a class="reference internal" href="lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a> ("," <a class="reference internal" href="lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a>)*
|
|
</pre>
|
|
<p>The <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement is a declaration which holds for the entire
|
|
current code block. It means that the listed identifiers are to be interpreted
|
|
as globals. It would be impossible to assign to a global variable without
|
|
<a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a>, although free variables may refer to globals without being
|
|
declared global.</p>
|
|
<p>Names listed in a <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement must not be used in the same code
|
|
block textually preceding that <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement.</p>
|
|
<p>Names listed in a <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement must not be defined as formal
|
|
parameters or in a <a class="reference internal" href="compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> loop control target, <a class="reference internal" href="compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a>
|
|
definition, function definition, or <a class="reference internal" href="#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement.</p>
|
|
<div class="impl-detail compound">
|
|
<p><strong>CPython implementation detail:</strong> The current implementation does not enforce the two restrictions, but
|
|
programs should not abuse this freedom, as future implementations may enforce
|
|
them or silently change the meaning of the program.</p>
|
|
</div>
|
|
<p id="index-40"><strong>Programmer’s note:</strong> the <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> is a directive to the parser. It
|
|
applies only to code parsed at the same time as the <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement.
|
|
In particular, a <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statement contained in a string or code
|
|
object supplied to the built-in <a class="reference internal" href="../library/functions.html#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a> function does not affect the code
|
|
block <em>containing</em> the function call, and code contained in such a string is
|
|
unaffected by <a class="reference internal" href="#global"><tt class="xref std std-keyword docutils literal"><span class="pre">global</span></tt></a> statements in the code containing the function
|
|
call. The same applies to the <a class="reference internal" href="../library/functions.html#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a> and <a class="reference internal" href="../library/functions.html#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a> functions.</p>
|
|
</div>
|