<div class="gmail_quote">On Mon, Jun 29, 2009 at 12:23 PM, Robert Lummis <span dir="ltr">&lt;<a href="mailto:robert.lummis@gmail.com">robert.lummis@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;">

Here&#39;s an example that seems not possible in python. I&#39;m probably<br>
missing something so please enlighten me. I only tried doing this as<br>
an exercise to show myself how name references work. I&#39;m not saying<br>
it&#39;s needed or that it&#39;s good practice.<br>
<br>
I can write the following as a single file and it works as expected:<br>
<br>
===snip===<br>
#!/usr/bin/python<br>
<br>
def show(*args):<br>
    print<br>
    for arg in args:<br>
            print arg + &#39;:&#39;,<br>
            exec(&#39;print &#39; + arg)<br>
<br>
a=15<br>
b=&#39;hello&#39;<br>
x=[&#39;bob&#39;,3]<br>
<br>
show(&#39;a&#39;)<br>
show(&#39;a&#39;,&#39;b&#39;)<br>
show(&#39;a&#39;,&#39;b&#39;,&#39;x&#39;)<br>
===snip===<br>
<br>
The calls to &#39;show&#39; output lines like &quot;a: 15&quot; which could be useful<br>
for debugging or some such purpose.<br>
<br>
However, it seems that I can&#39;t put the function definition in a file<br>
and import it because I can&#39;t find a way to refer to an object in the<br>
main program file from within a module file. </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> I understand that it&#39;s a<br>
good thing to contol which namespaces are referenced by which code but<br>
isn&#39;t there sometimes a need for code in a module to access the main<br>
program file&#39;s namespace? My example may be a little contrived but<br>
isn&#39;t this ability legitimately needed at times?</blockquote><div><br></div><div>I can&#39;t think of a single legitimate reason the module would need to access the object in the namespace in any way that isn&#39;t already provided. Especially in python it breaks the model of what objects are.</div>

<div><br></div><div>Strings (and integers AFAIK) are literals. Consider:</div><div><div>&gt;&gt;&gt; x[0] = &#39;c&#39;</div><div>Traceback (most recent call last):</div><div>  File &quot;&lt;stdin&gt;&quot;, line 1, in ?</div>

<div>TypeError: object doesn&#39;t support item assignment</div><div><br></div><div>When you create a string in c/c++ you have a string of characters, and you can edit it as such.</div><div>When you say knights = &#39;who say ni&#39; in python, you are referencing knights to the literal string &#39;who say ni&#39;. As far as python is considered, every single string combination already exists as an object, and your variable just references that object.</div>

<div><br></div><div>At least that&#39;s the way I&#39;ve understood it. So when you&#39;re trying to access the namespaces name for the object it&#39;s useless because there&#39;s nothing you can do with it. (And really it shouldn&#39;t be the responsibility of an outside function to modify the value of a variable anyway! It&#39;s also bad from a data security POV. You don&#39;t want functions messing with your data just by accident)</div>

<div><br></div><div>Anyway... that&#39;s as far as I&#39;ve been taught (and taught myself)... anyone notices any errors, please correct them!</div><div>HTH,</div><div>Wayne</div></div></div>