473,581 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Variable variable name" or "variable lvalue"

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(Mat erial1(12.5)+Ma terial2(25)+Mat erial3(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
11 2242
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
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(Mat erial1(12.5)+Ma terial2(25)+Mat erial3(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(sla b_arg)

Obviously not tested!

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

-Larry Bates
Aug 15 '07 #3
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(Mat erial1(12.5)+Ma terial2(25)+Mat erial3(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
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.appen d(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(Mat erial1(12.5)+Ma terial2(25)+Mat erial3(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(number s[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
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(Mat erial1(12.5)+Ma terial2(25)+Mat erial3(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
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
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(Mat erial1(12.5)+Ma terial2(25)+Mat erial3(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
in*****@gmail.c om 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(Ma terial1(12.5)+M aterial2(25)+Ma terial3(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
Gary Herron wrote:
in*****@gmail.c om 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(M aterial1(12.5)+ Material2(25)+M aterial3(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__.u pdate(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1718
by: Bob | last post by:
I usually use some "pre-load" code in my pages to preload graphics that will be swapped. But, I'm thinking that rather than the long, repetitive, once, for each graphic hardcoded stuff like this: var bb_off = new Image(); bb_off.src = "images/bb_off.jpg"; ....that I could have an array where I just listed the names of the graphics, a...
1
4063
by: sindre hiåsen | last post by:
Hi, I need a way to save a variable in a function. I want to do something like this. On functioncall set a variable and another function call gets the variable set by the other function call. Something like this.
2
1880
by: Alistair Bayley | last post by:
(.Net framework version 1.1.4322) The following XSL test case is rejected by System.Xml.Xsl.XslTransform, with an XsltException: "($dummy)+0 is an invalid XPath expression." If you remove the parentheses then it is happy. This appears to be an error; see http://www.w3.org/TR/xpath#NT-Expr. The path through the grammar is quite long; if you...
4
7089
by: Barry Edmund Wright | last post by:
Hi All, I am using the code below to assign a form name to a form variable (vFrm). Is there a way to assign the Openargs string directly to vFrm, i.e. vFrm = Openargs ? Private Sub Form_Load() Public vFrm As Form If Openargs = "subfrmProjects" then
7
3006
by: ben | last post by:
hello, an algorithm book i'm reading talks about the connectivity problem/algorithm. it gives a number of examples where the connectivity problem applies to real life situations (like, the objects may represent computers in a large network and the pairs represent connections between them) another real situation example that's given is:
6
1674
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why doesn't the global variable "divId" always refer to the element with id="divId"? ----------------------------------------------------------------------- Microsoft introduced a shortcut that can be used to reference elements which include an ID attribute...
11
2213
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the onloadListeners, addOnLoadListener below window?
0
8297
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new records there is an error "Incorrect syntax near '-'. Must declare the scalar variable "@UserName". I worked out in design view,code is automatically...
3
2835
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be programmatically set, this is necessary because I may have a simple table name or a union, hence I got to pick the correct one! Unfortunately, I...
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.