<div class="gmail_quote">On Mon, Jun 29, 2009 at 12:23 PM, Robert Lummis <span dir="ltr"><<a href="mailto:robert.lummis@gmail.com">robert.lummis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Here's an example that seems not possible in python. I'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'm not saying<br>
it's needed or that it'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 + ':',<br>
exec('print ' + arg)<br>
<br>
a=15<br>
b='hello'<br>
x=['bob',3]<br>
<br>
show('a')<br>
show('a','b')<br>
show('a','b','x')<br>
===snip===<br>
<br>
The calls to 'show' output lines like "a: 15" which could be useful<br>
for debugging or some such purpose.<br>
<br>
However, it seems that I can't put the function definition in a file<br>
and import it because I can'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's a<br>
good thing to contol which namespaces are referenced by which code but<br>
isn't there sometimes a need for code in a module to access the main<br>
program file's namespace? My example may be a little contrived but<br>
isn't this ability legitimately needed at times?</blockquote><div><br></div><div>I can't think of a single legitimate reason the module would need to access the object in the namespace in any way that isn'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>>>> x[0] = 'c'</div><div>Traceback (most recent call last):</div><div> File "<stdin>", line 1, in ?</div>
<div>TypeError: object doesn'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 = 'who say ni' in python, you are referencing knights to the literal string 'who say ni'. 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's the way I've understood it. So when you're trying to access the namespaces name for the object it's useless because there's nothing you can do with it. (And really it shouldn't be the responsibility of an outside function to modify the value of a variable anyway! It's also bad from a data security POV. You don't want functions messing with your data just by accident)</div>
<div><br></div><div>Anyway... that's as far as I've been taught (and taught myself)... anyone notices any errors, please correct them!</div><div>HTH,</div><div>Wayne</div></div></div>