Generate variables at run time?

holger krekel pyth at devel.trillke.net
Wed Jan 8 06:57:11 EST 2003


Byron Morgan wrote:
> Is there any way in Python to generate a new variable name based on data? I
> have tried the eval() function, but this gets rejected, if the variable
> doesn't already exist.

chances are that you need a 'bag' for your new variables.  Your program
can't possibly know in advance the names, anyway, can it? 

try to use a dictionary:

    b = dict(read_items())

where read_items returns a list of name-value tuples. 

and iterate through 'b' like so

    for name in b:
        print name, "=", b[name]
  
If you think you need something else, try to describe
your requirements as precise as possible.
 
regards,

    holger





More information about the Python-list mailing list