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)+Material 3(12.5)) #Slab is a
python class
I dont know how to automatize last piece of code for any x
thank you
Marcos 11 2135
On Wed, 15 Aug 2007 10:42:02 -0700, mfglinux wrote:
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
You want a dictionary.
M = dict()
for x in xrange(1, 4):
M[x] = Material(x)
Ciao,
Marc 'BlackJack' Rintsch
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)+Material 3(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
On 8/15/07, mfglinux <mf******@gmail.comwrote:
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)+Material 3(12.5)) #Slab is a
python class
I dont know how to automatize last piece of code for any x
thank you
Marcos
-- http://mail.python.org/mailman/listinfo/python-list
You could use a dictionary -- just build the dictionary keys using
your loop and assign values.
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)+Material 3(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
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)+Material 3(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
The solution with the dictionary worked perfectlly well, my script is
running and even produces data with sense!!!
Thank you very much indeed to all of you answering. Cheers!
On Aug 15, 1:42 pm, mfglinux <mfgli...@gmail.comwrote:
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)+Material 3(12.5)) #Slab is a
python class
I dont know how to automatize last piece of code for any x
thank you
Marcos
Regardless of whether or not this is a "best practice" sometimes it is
necessary. For example, I am looping through a dictionary to set some
class properties. Anyway, here is what I finally came up with:
exec "self.%s = '%s'" % (item, plist[item])
A more simple example for setting a variable outside of a class...
exec '%s = '%s'" % ('variableName', 'variable value')
Cheers!
Mike in*****@gmail.com wrote:
On Aug 15, 1:42 pm, mfglinux <mfgli...@gmail.comwrote:
>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)+Materia l3(12.5)) #Slab is a python class
I dont know how to automatize last piece of code for any x
thank you
Marcos
Regardless of whether or not this is a "best practice" sometimes it is
necessary. For example, I am looping through a dictionary to set some
class properties. Anyway, here is what I finally came up with:
exec "self.%s = '%s'" % (item, plist[item])
Yuck! Not at all necessary. Use setattr instead:
setattr(self, item, plist[item])
That's much cleaner then an exec or eval. You may also find getattr and
hasattr useful.
Gary Herron
A more simple example for setting a variable outside of a class...
exec '%s = '%s'" % ('variableName', 'variable value')
Cheers!
Mike
Gary Herron wrote:
in*****@gmail.com wrote:
>On Aug 15, 1:42 pm, mfglinux <mfgli...@gmail.comwrote:
>>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)+Materi al3(12.5)) #Slab is a python class
I dont know how to automatize last piece of code for any x
thank you
Marcos
Regardless of whether or not this is a "best practice" sometimes it is necessary. For example, I am looping through a dictionary to set some class properties. Anyway, here is what I finally came up with:
exec "self.%s = '%s'" % (item, plist[item])
Yuck! Not at all necessary. Use setattr instead:
setattr(self, item, plist[item])
That's much cleaner then an exec or eval. You may also find getattr and
hasattr useful.
Or even, in some cases,
self.__dict__.update(otherdict)
if you have a dictionary of stuff to put into an object.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
On Aug 19, 1:10 pm, Gary Herron <gher...@islandtraining.comwrote:
inmm...@gmail.com wrote:
On Aug 15, 1:42 pm, mfglinux <mfgli...@gmail.comwrote:
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)+Material 3(12.5)) #Slab is a
python class
I dont know how to automatize last piece of code for any x
thank you
Marcos
Regardless of whether or not this is a "best practice" sometimes it is
necessary. For example, I am looping through a dictionary to set some
class properties. Anyway, here is what I finally came up with:
exec "self.%s = '%s'" % (item, plist[item])
Yuck! Not at all necessary. Use setattr instead:
setattr(self, item, plist[item])
That's much cleaner then an exec or eval. You may also find getattr and
hasattr useful.
Gary Herron
A more simple example for setting a variable outside of a class...
exec '%s = '%s'" % ('variableName', 'variable value')
Cheers!
Mike
Thanks! I'm still getting used to Python's nifty features.
On Aug 15, 4:19 pm, Peter Otten <__pete...@web.dewrote:
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)
Better to use the `sum' builtin and a generator expression:
period = Slab(sum(Material(x)) for x in numbers)
--
Roberto Bonvallet This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Bob |
last post: by
|
1 post
views
Thread by sindre hiåsen |
last post: by
|
2 posts
views
Thread by Alistair Bayley |
last post: by
|
4 posts
views
Thread by Barry Edmund Wright |
last post: by
|
7 posts
views
Thread by ben |
last post: by
|
6 posts
views
Thread by FAQ server |
last post: by
|
11 posts
views
Thread by gg9h0st |
last post: by
|
reply
views
Thread by roamnet |
last post: by
|
3 posts
views
Thread by Andrea Raimondi |
last post: by
| | | | | | | | | | |