variable hell

Adriaan Renting renting at astron.nl
Mon Aug 29 03:17:39 EDT 2005


I'm sorry for top-posting and not marking quoted text, but my e-mail reader (Novell Groupwise 6 for Linux) does not mark quoted text. The only thing it does is the >>> $NAME <$EMAIL> $DATE >>> above the quoted text. Therefore I add my comments at the top. The only alternative I have is copying all text to an editor, adding quote marks by hand, then copying it back into a new e-mail message, but this takes to much of m time. If this is to confusing I will just stop posting in this list.

My original code was: exec(eval("'a%s=%s' % (count, value)"))
Then Rafi said: exec("'a%s=%s' % (count, value)")
To which I responded that his example does not work in my Python, and I think it's invalid.
Then Martin came with: exec 'a%s = %s' % (count, value)
This does work.
But he seems to keep telling me I'm quoting him wrong, while I'm quoting Rafi.

Furthermore I'm going with your suggestion of checking the imp, globals() and locals().

Adriaan Renting. 
 
>>>Mike Meyer <mwm at mired.org> 08/27/05 2:24 am >>> 
[The context is totally hosed by top posting and failure to mark 
quoted text. I gave up on recovering it.] 
 
"Adriaan Renting" <renting at astron.nl> writes: 
 
>Not in my Python. 
> 
>>>>for count in range(0, 10): 
>...     value = count 
>...     exec("'a%s=%s' % (count, value)") 
 
You left in the extra set of quotes. Try this: 
 
>>>for count in range(10): 
...  value = count 
...  exec 'a%s = %s' % (count, value) 
... 
>>>dir() 
['__builtins__', '__doc__', '__file__', '__name__', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'count', 'help', 'readline', 'rlcompleter', 'sys', 'value'] 
 
I also removed the extraneous parens that you and Rafi both used - 
exec is a statement, not a function. 
 
Of course, your two examples are better done using imp and globals() or 
locals(). 
 
         <mike 
 
>>>>dir() 
>['__builtins__', '__doc__', '__name__', 'count', 'value'] 
>>>>for count in range(0, 10): 
>...     value = count 
>...     exec(eval("'a%s=%s' % (count, value)")) 
>... 
>>>>dir() 
>['__builtins__', '__doc__', '__name__', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'count', 'value'] 
>>>> 
> 
>I myself use code like this to load user defined classes. 
>            exec(eval("'from %s import %s' % (script, script)")) 
>            exec(eval("'self.user_class = %s()' % script")) 
>            self.user_class.run() 
> 
>But this can probably be done with the imp module too. 
>  
>>>>rafi <rafi at free.fr> 08/25/05 6:03 pm >>> 
>Adriaan Renting wrote: 
>>You might be able to do something along the lines of 
>> 
>>for count in range(0,maxcount): 
>> value = values[count] 
>> exec(eval("'a%s=%s' % (count, value)")) 
>  
>why using the eval? 
>  
>exec ('a%s=%s' % (count, value)) 
>  
>should be fine 
>  
>-- 
>rafi 
>  
>"Imagination is more important than knowledge." 
>                           (Albert Einstein) 
>-- 
>http://mail.python.org/mailman/listinfo/python-list 
> 
 
-- 
Mike Meyer <mwm at mired.org>http://www.mired.org/home/mwm/ 
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. 
-- 
http://mail.python.org/mailman/listinfo/python-list 




More information about the Python-list mailing list