<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Jan 8, 2010, at <span class="Apple-style-span" style="font-family: Helvetica; ">7:35:39 PM EST</span>, <span class="Apple-style-span" style="font-family: Helvetica; ">Terry Reedy <<a href="mailto:tjreedy@udel.edu">tjreedy@udel.edu</a>></span> wrote:</div><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" color="#00007D" face="Helvetica"><b><br></b></font></div>On 1/8/2010 12:02 PM, Mitchell L Model wrote:<br><br><br><blockquote type="cite">On further reflection, I will add that<br></blockquote><blockquote type="cite">what appears to be happening is that during import both the global and<br></blockquote><blockquote type="cite">local dictionaries are set to a copy of the globals() from the importing<br></blockquote><blockquote type="cite">scope and that copy becomes the value of the module's __dict__ once<br></blockquote><blockquote type="cite">import has completed successfully.<br></blockquote><br>I have no idea why you think that. The module dict starts empty except for __name__, __file__, and perhaps a couple of other 'hidden' items. It is not a copy and has nothing to do with importing scopes.<br></span></blockquote><div><br></div><div>Why I think -- or, rather, thought -- that was because of some defective experiments I ran. It was purely a delusion. Thank you for correcting it.</div><br><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><br>> and that copy becomes the value of the module's __dict__ once<br>> import has completed successfully.<br><br>That new dict becomes .... .<br><br><blockquote type="cite">Because exec leaves locals() and globals() distinct,<br></blockquote><br>Not necessarily.<br><br>In 3.x, at least,<br>exec(s)<br>executes s in the current scope. If this is top level, where locals is globals, then same should be true within exec.<br></span></blockquote><div><br></div><div>Yes. To simplify some of my ramblings and incorporate the points you and others have made, and to once again acknowledge Python's elegance, an important observation which I bet even a lot of serious Python programs don't realize (or at least not consciously) is that:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>globals() is locals()</div><div>in the following contexts:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>the interpreter top level</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>the top level of a module (though as you point out, starts out as a very bare dictionary during import)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>a string being exec'd when the call to exec includes</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>no dictionary argument(s)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>one dictionary argument</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>the same dictionary as both the second and third arguments</div><div><div>The identity does not hold for:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>a string being exec'd when a different dictionary is provided as the second and third arguments to exec</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>inside anything that creates a scope: a function definition, class definition, etc.</div><div><br></div><div>Did I get all that right? Are there any other contexts that should be included in these?</div></div><br><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><br>d = {}<br>exec(s, d)<br><br>In 3.x, at least, d will also be used as locals.<br></span></blockquote><div><br></div>Yes, talking about 3.x.</div><div><br><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><br>exec(s, d, d)<br><br>Again, globals and locals are not distinct.<br><br>It would seem that in 3.x, the only way for exec to have distinct globals and locals is to call exec(s) where they are distinct or to pass distince globals and locals.<br></span></blockquote><div><br></div><div>Apparently so. To clarify "where they are distinct", that would mean from a context in which they were already distinct, which is not the case if exec is called from the top level, but is the case if called from within, say, a function, as my code does.</div><div><br></div><br><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><br>Some of the issues of this thread are discussed in Language Reference 4.1, Naming and Binding. I suppose it could be clearer that it is, but the addition of nonlocal scope complicated things.<br></span></blockquote></div><div><br></div><div>I pretty much have that section memorized and reread it at least monthly. It's part of what I meant by starting my original comments by saying that I thought I understood all of this. Thank you (and others) for helping clarify exactly what's going on. As with so many things in Python, it is not always easy to keep one's preconceptions, delusions, and experiences with other languages out of the way of its simplicity, even if one is a very experienced and knowledgeable Python programmer.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>--- Mitchell</div></body></html>