"Variable variable name" or "variable lvalue"

Dan Stromberg - Datallegro dstromberg at datallegro.com
Thu Aug 16 01:26:39 CEST 2007


On Wed, 15 Aug 2007 10:42:02 -0700, mfglinux wrote:

> Hello to everybody
> 
> I would like to know how to declare in python a "variable name" that
> it is in turn a variable
> In bash shell I would wrote sthg like:
> 
> for x in `seq 1 3`
> do
>   M$i=Material(x)  #Material is a python class
> done
> 
> Why I need this? Cause I have a python module that obliges me to build
> a variable called Period, which should have a variable name of
> summands (depends on the value of x)
> 
> #Let's say x=3, then Period definition is
> Period=Slab(Material1(12.5)+Material2(25)+Material3(12.5)) #Slab is a
> python class
> 
> I dont know how to automatize last piece of code for any x
> 
> thank you
> 
> Marcos

It sounds to me like you want python's "eval", based on my
understanding of how that bash code should work.  It's not that different
from eval in bash, though in python you'd need it; in bash you don't in
this case.

So you could probably do something like (untested):

for x in xrange(1,4):
	eval 'M%d=Material(x)' % x





More information about the Python-list mailing list