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
In Python you would build a list instead of inventing variable names: numbers = [12.5, 25, 12.5] materials = [] for x in numbers: materials.append(Material(x)) > 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 You can use another loop to to "sum" over the materials and then feed the result to the Slab constructor: accu = materials[0] for material in materials[1:]: accu += material period = Slab(accu) If you want to simplify things somewhat you can merge the two loops into one: numbers = [12.5, 25, 12.5] accu = Material(numbers[0]) for x in numbers[1:]: accu += Material(x) period = Slab(accu) Or you try your hands on a bit of functional programming: from operator import add numbers = [12.5, 25, 12.5] period = Slab(reduce(add, (Material(x) for x in numbers))) Peter -- http://mail.python.org/mailman/listinfo/python-list