[GRASS-dev] python question

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)

Thanks
Michael


C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Consortium for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

Michael Barton wrote

for i in flist:
  's%_new' % i = 10

where flist is a list of strings (e.g., file names)

Being not a programmer and also not sure, what you exactly want to do. But
AFAIK string replacement in Python should be coded '%s' instead of 's%'.
And '%i' is actually reserved for the replacement of integers, but you are
about to use it as an iterator as well, isn't it? Would be interesting, if
this works nevertheless ...

Regards, Christine

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/python-question-tp4983356p4983365.html
Sent from the Grass - Dev mailing list archive at Nabble.com.

Hi,

On Sat, Jun 23, 2012 at 9:48 PM, Michael Barton <michael.barton@asu.edu> 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 find answer here:

http://stackoverflow.com/questions/2320945/python-using-vars-to-assign-a-string-to-a-variable

example of using function vars is in wxpython/gui_core/toolbars.py, line 130

Anna

Thanks
Michael

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Consortium for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

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 <glynn@gclements.plus.com>

Thanks Anna and Glynn,

I got the same advice from a colleague here: use a dictionary. Works perfectly to resolve the problem.

Michael Barton
School of Human Evolution &Social Change
Center for Social Dynamics & Complexity
Arizona State University

...Sent from my iPad

On Jun 24, 2012, at 1:56 AM, "Anna Kratochvílová" <kratochanna@gmail.com> wrote:

Hi,

On Sat, Jun 23, 2012 at 9:48 PM, Michael Barton <michael.barton@asu.edu> 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 find answer here:

Python: Using vars() to assign a string to a variable - Stack Overflow

example of using function vars is in wxpython/gui_core/toolbars.py, line 130

Anna

Thanks
Michael

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Consortium for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
grass-dev Info Page