variable hell

Adriaan Renting renting at
Fri Aug 26 08:56:15 CEST 2005

Not in my Python.

>>> for count in range(0, 10):
...     value = count
...     exec("'a%s=%s' % (count, value)")
>>> 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"))

But this can probably be done with the imp module too.
>>>rafi <rafi at> 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 
"Imagination is more important than knowledge." 
                           (Albert Einstein) 

More information about the Python-list mailing list