"Variable variable name" or "variable lvalue"

Larry Bates larry.bates at websafe.com
Wed Aug 15 15:11:56 EDT 2007


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
> 
I think the answer is to use a dictionary of lists , but it is a little hard to
tell from your description:

mdict={1:[Material1, 12.5],
       2:[Material2, 25.0],
       3:[Material3, 12.5]
      }

x=3
slab_arg=0
for i in range(1,x):
    func, farg=mdict[i]
    slab_arg+=func(farg)

Period=Slab(slab_arg)

Obviously not tested!

I sense that the Material functions should be consolidated into something more
general here.

-Larry Bates



More information about the Python-list mailing list