Michael Barton wrote: > This is probably simple but I'm missing something somewhere. How can > I dynamically create variable names and then assign values to the > variables? I need to do something along the lines of: > > for i in flist: > 's%_new' % i = 10 > > where flist is a list of strings (e.g., file names)
You can create new global (module-scope) variables by adding them to the dictionary returned by the globals() function. You cannot dynamically create local variables, as the parser has to find the variable in the function body at parse time in order for it to be treated as a local variable. You can retrieve a dictionary of local variables using the locals() function, but the result should not be modified. You can dynamically get/set object members using getattr() and setattr(). But you should really try to avoid accessing variables dynamically, as it makes the code harder to follow (e.g. grepping for occurrences of "something_new" won't find the above). Wherever possible, use dictionaries instead. -- Glynn Clements <[email protected]> _______________________________________________ grass-dev mailing list [email protected] http://lists.osgeo.org/mailman/listinfo/grass-dev
