[Tutor] python $i equivalent ?

Paul Worrall paul at basilisk.ukfsn.org
Thu Jun 10 14:56:34 EDT 2004


On Wednesday 09 Jun 2004 22:25, Michele Alzetta wrote:
> Suppose I wanted to create numerous instances of a class
> and name each istance according to an element in a list ?
>
> for element in list:
>     element = Class()
>
> doesn't work of course; I need something like the bash '$'
> stuff here
>
> for element in list:
>     $element = Class()
>
> (of course this is neither bash nor python, I know !)
>
> How would this be done ?

Does this do what you want?

for element in list:
    exec element + " = Class()"

this compiles a Python statement as a string and passes it to the exec 
built-in function for execution.  list should contain only strings.

-- 
Paul



More information about the Tutor mailing list