0

This may be a very simple question, but how can I use a string for the name of a class/object declaration? I'm working with PySide, and I have code that will make a text input for every entry in an array.

i=0
d = {}
for name in mtlName:
    i = i+1
    curOldLabel = d["self.oldLabel" + str(i)]

So now I have to just decalre QtGui.QLineEdit() as what curOldLabel equals (self.oldLabel1 = QtGui.QLineEdit(), self.oldLabel2 = QtGui.QLineEdit(), etc). How do I tell it not to overwrite curOldLabel, but instead use the string as the name for this object?

1 Answers1

3

Your best bet is to use another dictionary to store those objects. It's safe, it's easy to use and it has fast lookup. You don't want to be creating normal variables with dynamic names in most scenarios.

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
  • This is what I get when trying to use a dictionary.. http://i.imgur.com/Gm7zoY9.png Note how it returns what I want first, but then when I attempt to assign the name, it overwrites newBox1 with the hex data instead – Gamerman12 Jul 26 '14 at 20:34
  • You're trying to use dynamic variable names for both `newBox1` and `newLabel1`, and only one of those was converted to a dictionary. The `self` object you're calling should have its own personal dictionary, one of the keys for which is `newLabel1`. – TheSoundDefense Jul 26 '14 at 20:37