Variable names on the fly?
Terry Reedy
tjreedy at udel.edu
Thu Nov 14 15:29:00 EST 2002
"e-tones.co.uk" <admin at e-tones.co.uk> wrote in message
news:3dd3f5fd$0$28311$fa0fcedb at lovejoy.zen.co.uk...
> Hi all, does python allow the creation of variable names on the fly,
more
> importantly lists.
>
> I have a stock list containing 5 elements, [0,0,0,0,0]
>
> Lets call it: list
>
> Now, via a loop, can I create list1, list2, list3 on the fly by
replicting
> the original list, eg
>
> i = 1
> for i in range(2)
> list+i = list
>
> Whats the proper format to concatenate (sp?) variable names. I used
list+i,
> obviously this doesnt work, but what does :)
I believe you can use setattr() with globals, but you almost certainly
do not want to do this. How do you access the runtime-defined names?
As suggested, an explicit dict others than globals is almost certainly
better. TJR
More information about the Python-list
mailing list