programmatically define a new variable on the fly
Ian Clark
iclark at mail.ewu.edu
Thu Aug 9 18:36:09 EDT 2007
Lee Sander wrote:
> Hi,
>
> I would like to define a new variable which is not predefined by me.
> For example,
> I want to create an array called "X%s" where "%s" is to be determined
> based on the data I am processing. So, for example, if I the file
> I'm reading has
> g 99
> on the first line, I want to create a new variable called "Xg" whose
> length
> is 99.
> I tried eval("Xg=[0]*99") but that did not work.
>
> any help would be greatly appreciated
> Lee
>
>>> Xg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Xg' is not defined
>>> namespace = globals()
>>> var_name = 'Xg'
>>> var_value = [0] * 9
>>> namespace[var_name] = var_value
>>> Xg
[0, 0, 0, 0, 0, 0, 0, 0, 0]
Ian
More information about the Python-list
mailing list