By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

"Variable variable name" or "variable lvalue"

P: n/a
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

Aug 15 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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
Aug 15 '07 #2

P: n/a
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
Aug 15 '07 #3

P: n/a
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.
Aug 15 '07 #4

P: n/a
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
Aug 15 '07 #5

P: n/a
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
Aug 15 '07 #6

P: n/a
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!

Aug 16 '07 #7

P: n/a
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

Aug 19 '07 #8

P: n/a
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

Aug 19 '07 #9

P: n/a
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 -------------

Aug 20 '07 #10

P: n/a
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.

Sep 12 '07 #11

P: n/a
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

Sep 12 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.