variable hell

Adriaan Renting renting at astron.nl
Fri Aug 26 10:48:26 CEST 2005


I was responding to rafi's suggestion, I had not received the "exec 'a%s = %s' % (count,count)" response yet at that time. The "exec 'a%s = %s' % (count,value)" works fine.

>Not in my Python.
>
>---snip---
>
>why using the eval? 
> 
>exec ('a%s=%s' % (count, value)) 
> 
>should be fine 
> 
>-- 
>rafi 

--- I appologize for any top-posting, and improper inlining, I'm using groupwise ---
 
>>>"Martin v. Löwis" <martin at v.loewis.de> 08/26/05 10:19 am >>> 
Adriaan Renting wrote: 
>Not in my Python. 
> 
> 
>>>>for count in range(0, 10): 
> 
>...     value = count 
>...     exec("'a%s=%s' % (count, value)") 
>... 
> 
>>>>dir() 
> 
>['__builtins__', '__doc__', '__name__', 'count', 'value'] 
 
You did not copy the suggestion properly: 
 
>>>for count in range(0, 10): 
...   exec 'a%s = %s' % (count,count) 
... 
>>>dir() 
['__builtins__', '__doc__', '__file__', '__name__', 'a0', 'a1', 'a2', 
'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'count'] 
>>>a5 
5 
 
(you can put additional parentheses around the string, but not 
additional quotation marks) 
 
Regards, 
Martin 
-- 
http://mail.python.org/mailman/listinfo/python-list 





More information about the Python-list mailing list