A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20100526/0c536c93/attachment-0001.html below:

<br><br><div class="gmail_quote">On Wed, May 26, 2010 at 11:48, Mark Dickinson <span dir="ltr">&lt;<a href="mailto:dickinsm@gmail.com">dickinsm@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Wed, May 26, 2010 at 10:15 AM, Colin H &lt;<a href="mailto:hawkett@gmail.com">hawkett@gmail.com</a>&gt; wrote:<br>
&gt; Â  issue991196 was closed being described as intentional. Â I&#39;ve added<br>
&gt; a comment in that issue which argues that this is a serious bug (also<br>
&gt; aserted by a previous commenter - Armin Rigo), because it creates a<br>
&gt; unique, undocumented, oddly behaving scope that doesn&#39;t apply closures<br>
&gt; correctly. At the very least I think this should be acknowledged as a<br>
&gt; plain old bug (rather than a feature), and then a discussion about<br>
&gt; whether it will be fixed or not.<br>
<br>
</div>Here&#39;s a quick recap of the issue so that people don&#39;t have to go<br>
searching through the bug archive. Â In Python 2.x, we get the<br>
following behaviour:<br>
<br>
&gt;&gt;&gt; code = &quot;&quot;&quot;\<br>
... y = 3<br>
... def f():<br>
... Â  Â  return y<br>
... f()<br>
... &quot;&quot;&quot;<br>
&gt;&gt;&gt; exec code in {} Â  # works fine<br>
&gt;&gt;&gt; exec code in {}, {} Â  # dies with a NameError<br>
Traceback (most recent call last):<br>
 Â File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>
 Â File &quot;&lt;string&gt;&quot;, line 4, in &lt;module&gt;<br>
 Â File &quot;&lt;string&gt;&quot;, line 3, in f<br>
NameError: global name &#39;y&#39; is not defined<br>
<br>
The issue is whether the second example should work, given that two<br>
different dictionaries have been passed.<br>
<br>
The cause of the NameError can be seen by looking at the bytecode: y<br>
is bound using STORE_NAME, which stores y into the locals dictionary<br>
(which here is *not* the same as the globals dictionary) but the<br>
attempt to retrieve the value of y uses LOAD_GLOBAL, which only looks<br>
in the globals.<br>
<br>
&gt;&gt;&gt; co = compile(code, &#39;mycode&#39;, &#39;exec&#39;)<br>
&gt;&gt;&gt; dis.dis(co)<br>
 Â 1 Â  Â  Â  Â  Â  0 LOAD_CONST Â  Â  Â  Â  Â  Â  Â  0 (3)<br>
 Â  Â  Â  Â  Â  Â  Â 3 STORE_NAME Â  Â  Â  Â  Â  Â  Â  0 (y)<br>
<br>
 Â 2 Â  Â  Â  Â  Â  6 LOAD_CONST Â  Â  Â  Â  Â  Â  Â  1 (&lt;code object f at<br>
0xa22b40, file &quot;mycode&quot;, line 2&gt;)<br>
 Â  Â  Â  Â  Â  Â  Â 9 MAKE_FUNCTION Â  Â  Â  Â  Â  Â 0<br>
 Â  Â  Â  Â  Â  Â  12 STORE_NAME Â  Â  Â  Â  Â  Â  Â  1 (f)<br>
<br>
 Â 4 Â  Â  Â  Â  Â 15 LOAD_NAME Â  Â  Â  Â  Â  Â  Â  Â 1 (f)<br>
 Â  Â  Â  Â  Â  Â  18 CALL_FUNCTION Â  Â  Â  Â  Â  Â 0<br>
 Â  Â  Â  Â  Â  Â  21 POP_TOP<br>
 Â  Â  Â  Â  Â  Â  22 LOAD_CONST Â  Â  Â  Â  Â  Â  Â  2 (None)<br>
 Â  Â  Â  Â  Â  Â  25 RETURN_VALUE<br>
&gt;&gt;&gt; dis.dis(co.co_consts[1]) Â # disassembly of &#39;f&#39;<br>
 Â 3 Â  Â  Â  Â  Â  0 LOAD_GLOBAL Â  Â  Â  Â  Â  Â  Â 0 (y)<br>
 Â  Â  Â  Â  Â  Â  Â 3 RETURN_VALUE<br>
<br>
This is a long way from my area of expertise (I&#39;m commenting here<br>
because it was me who sent Colin here in the first place), and it&#39;s<br>
not clear to me whether this is a bug, and if it is a bug, how it<br>
could be resolved. Â What would the impact be of having the compiler<br>
produce &#39;LOAD_NAME&#39; rather than &#39;LOAD_GLOBAL&#39; here?</blockquote><div><br></div><div>It wouldn&#39;t matter. The &#39;f&#39; function only knows about its own namespace (separate from the surrounding code&#39;s local namespace), and the global namespace. LOAD_NAME is only different from LOAD_GLOBAL in that it looks in the local namespace first, but in this case the local namespace contains nothing. Here&#39;s what happens:</div>
<div><br></div><div>&#39;exec code in d1, d2&#39; executes code with &#39;d1&#39; as locals and &#39;d2&#39; as globals. Assignment always operates on the local namespace (barring the &#39;global&#39; declaration.) The function definition also assigns to the local namespace, but the created function knows nothing about that local namespace -- it only cares about its own namespace and the global namespace, &#39;d1&#39;.</div>
<div><br></div><div>&#39;exec code in d1&#39; does the same thing as &#39;exec code in d1, d1&#39;: it uses the same dict for the locals and the globals. The execution of the code doesn&#39;t change -- the assignment to &#39;y&#39; still assigns to the locals, and the &#39;f&#39; function still looks it up in globals, but now *they are the same dict*. Using the same dict for locals and globals is how modules work, as well.</div>
<div><br></div><div>The main confusion here is the fact that &#39;exec&#39; doesn&#39;t generate closures. (Nobody was ever confused about this behaviour back in Python 2.0-and-earlier! :-) The reason for that is the disconnect between the compiler and the exec statement: the compiler sees no enclosing function, so it doesn&#39;t generate a closure. The exec statement, because it gets two different namespaces, then executes it like a function, with a distinct local namespace.</div>
<div><br></div></div>-- <br>Thomas Wouters &lt;<a href="mailto:thomas@python.org">thomas@python.org</a>&gt;<br><br>Hi! I&#39;m a .signature virus! copy me into your .signature file to help me spread!<br>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4