"Variable variable name" or "variable lvalue"

M

mfglinux

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
 
M

Marc 'BlackJack' Rintsch

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
 
L

Larry Bates

mfglinux said:
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
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
 
S

Shawn Milochik

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


You could use a dictionary -- just build the dictionary keys using
your loop and assign values.
 
P

Peter Otten

mfglinux said:
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
 
D

Dan Stromberg - Datallegro

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
 
M

mfglinux

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!
 
I

inmmike

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

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
 
G

Gary Herron

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

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
 
S

Steve Holden

Gary said:
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
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 -------------
 
I

inmmike

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.
 
R

Roberto Bonvallet

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top