[Tutor] create 1000 000 variables
Michael P. Reilly
arcege at gmail.com
Sat Jul 15 21:23:02 CEST 2006
On 7/14/06, Сергій <kyxaxa at gmail.com> wrote:
>
> suppose I need to create 1000 000 variables
> var_1, var_2, .... var_1000000
>
> how to do this using for?
> (something like
> for i in range(1000000):
> ___var_
>
>
You should think about not creating variables like this, it is bad
programming and continuing to use similar techniques leads you down the path
of buggy code and hours of trying to trace your code.
That being said, here's a nice, safe solution:
# get the current module
import sys
cur_module = sys.module[__name__]
for i in range(1000000):
cur_module['var_%s' % i] = i # var_i = i
print var_46889 # in the current namespace
But again, like others have suggested, you should rethink your problem and
your solution before starting down your path. What are you really
capturing?
-Arcege
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060715/ebdb45c9/attachment.html
More information about the Tutor
mailing list