View Full Version : Dynamic variable substitutions
darelf
07-03-2002, 10:58 AM
Before I started picking up Python, I used Tcl.
In tcl, I can do variable name substitutions on the fly:
atest = 1
btest = 2
ctest = 3
mylist = [a, b, c]
print $mylist[0]test
gives the result 1
Is there a method of doing this in Python, that isn't too cumbersome?
inkedmn
07-03-2002, 11:19 AM
i don't think that's possible in python, but i can't say for sure...
uh. I bet you can, using dict somehow or something or eval ... but uh.. is it necessary?
Yeh; works with eval:
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> atest = 1
>>> eval('atest')
1
>>> b = 'a'
>>> eval(b+'test')
1
>>>
inkedmn
07-03-2002, 11:21 AM
/me s1 t3h Wr0ng!!!1
:D:D:D
http://www.codemonkeyx.net/images/monkey_smile.jpg
wtf? why is this showing the url, and not the image?
darelf
07-03-2002, 11:54 AM
Ah, excellent!!
That's perfect... eval, haven't played with that before... learn something new every day....
Thanks, guys...
EDIT: to the "is it necessary" question...
Not sure if "necessary" is the right word, just the easiest for me.
In that project I posted about, there are 10 stats. But each of the ten stats have several numbers associated with them. Which would be fine, except that I need to display each of those numbers in their own sections of the panel. So I have 10 labels for each of the numbers that need to be displayed.
i.e. for Agility, I have labels AgTemp, AgPot, AgBasic, AgRacial, AgSpec, AgTotal. If I have a list of stats (i.e. ['Ag', 'Co', etc]) then I just foreach through them and do substitutions:
for x in stats:
y = eval( 'self.'+x+'Temp' )
y.setText( str(pc.stats[x].temp) )
In this case I could even do a double substitution to change the entire display with a list of stat names and a list of label "types" ( ['Temp', 'Pot', etc] )
Saves my fingers, at least....
And it cuts the length of the code tremendously, which goes towards readability, and it also adds to the dynamic nature (if I need to add or remove a stat, it is very easy to do)
Again, Thanks
jemfinch
07-03-2002, 01:58 PM
Originally posted by darelf
Is there a method of doing this in Python, that isn't too cumbersome?
I sure hope doing something this crazy is as cumbersome as possible in Python.
Use a dictionary. Use an array. This "technique" you learned in Tcl is horrible programming style and was probably only "developed" to workaround Tcl's poor performance.
I repeat, it's horrible, awful, send-chills-down-the-back-of-any-serious-programmer nasty.
Jeremy
darelf
07-03-2002, 04:19 PM
Originally posted by jemfinch
I sure hope doing something this crazy is as cumbersome as possible in Python.
Use a dictionary. Use an array.
Ack. And double Ack.
If I did that with Qt, then the files wouldn't be portable.
The actual information is stored in a dict inside of the relevant class. All of the numbers are stored "sanely". The problem is mapping from the variables to the Qt widgets. If I were to put the Qt widgets in a python array, then my portability of the Qt part is lost. I wouldn't be able to reuse to .ui files with, say C++ or Ruby or Java or whatever other language binding there might be.
I need to quickly and efficiently map from the information to the appropriate Qt widget on screen. If there were a way to have "live" variables (there might be, but I haven't found it) then I would use that. But since I have to deal with what I have (Python mated with Qt) I have to deal with the limitations.
If you have an actual suggestion, I'll gladly look at it.
This "method" of assigning variables is all I have to go on and still keep the entire thing dynamic. I you have a simpler/more elegant solution, I would love to get my hands on it, since I could use such a thing in more than one project.
EDIT:
The above may have sounded rude... I really am interested in a new method, since I've run into this problem more than once and don't have a good solution.
file13
07-03-2002, 05:57 PM
well yeah that's pretty bad doing something like this. but it does say something about the flexibility of these languages. in CL you can do:
[1]> (setq atest 1)
1
[2]> (setq btest 2)
2
[3]> (eval (intern (substitute #\B #\A (symbol-name 'atest))))
2
[4]>
it's kind of interesting because in Lisp a symbol is a type like integer (which is why line 3 is so long because we have to convert to a string and back to a symbol) where in Python it get's the variable name from the string.
>>> atest = 1
>>> eval("atest")
1
where in Lisp it's gonna be the if you "atest" in eval it would spit back "atest". interesting.....sorry, kinda off topic but this brings up some interesting stuff on the ways these languages evaluate..... :)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.