View Full Version : Python Variables
nobody
01-06-2007, 03:07 PM
Do variable variables exist in python?
I've done a lot of searches and haven't found anything about them either way, so I'm guessing not.
If there's not what are my options for dynamically created variables, if any?
Krilnon
01-06-2007, 03:27 PM
By 'variable variables' do you mean that the identifier is variable? For example, (in ActionScript) you could create an identifier 'mc+i' and later refer to 'mc1'?
If so, you can use lists or dictionaries in Python. From what I understand, lists are essentially arrays, and dictionaries are used much like the array access operator was used in AS2 (but any immutable object can be used as a key, not just a string).
With a dictionary, you might have something like: colors = {}
colors['ball1'] = 'red'
colors['ball2'] = 'blue''ball1' and 'ball2' could be accessed with "colors['ball1']", etc. later. Since identifiers are only really used by the programmer, some situations don't really call for unique identifiers. For example, if you were making 20 text fields, you might be tempted to refer to them (in a loop) as "'textfield'+i', when it might be just as easy to use an array: "textfields[i]". I hope that's what you're asking. :sure:
nobody
01-06-2007, 03:50 PM
Yeah that's what I'm asking basically. I'm already using lists, hadn't thought of using dictionaries. Actually one giant list would probably work for what I'm doing. See I'm making a Sudoku solver to get back up to speed with Python so I have 81 cells that each have a possibility of 9 different answers, and these cells have possible answers removed by a number of functions I've written.
However right now I have it set up where I essentially run each function once per cell, so it's all hard coded and my file is something like 2,000 lines long, where variable variables could make it something like 100 - 200 I believe.
I'm going to try your method and see if I can come up with something, thank you sir.
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.