I've used something along the lines for Pythoncard, but the names are
generated within strict rules and expectations.

i.e. 

first off â 

#Some code that uses regExs, finds all headings, and    
# a asterisk to indicate a date value, returns a iterable object
reHead using finditer()
#which contains two groups, head and date
#date is either a '' or '*' which evals to False/ True for my purposes
#The asterisk isn't important, it's just a non-null.


counter = 0 # I use nums as keys for easy sort/increment
dex={}

for item in reHead:
      dex[counter]={'title':item.group('head'),
                              'date': item.group('date') }
       counter += 1

j=cPickle.Pickler(someOpenFile)
j.dump(dex)
someOpenFile.close()

####

x=cPickle.Unpickler(someOpenFile)
dex2 = x.read()

for key in dex2.keys():
      buttName = '%dbutton' % key
      choiceName = %dchoice % key

      if dex2[key]['date']:
          choiceList=['Past', Present', 'Future']
      else:
           choiceList = ['Highest', 'Lowest', 'Average']

      self.components[buttName] = {
                                                        'type': 'button',
                                                        'name': buttName,
                                                        'label' :
dex2[key]['title']
                                                         â..
                                                          }

      self.components[choiceName] = {
                                                            'type': 'choice',
                                                            'name': choiceName,
                                                            'label' :
"Select option",
                                                            'items' :
choiceList,
                                        'stringSelection': choiceList[0] 
                                                             }

 
Only reason I do it this way, as opposed to using a list, is a stylistic choice.
But yeah, you end up iterating a whole lot, and unless the variable
names are generated
predictably, you have a hard time referencing stuff.
So, in conclusion, dynamically named variables are very limited in
their usefulness.



On Thu, 27 Jan 2005 09:21:51 -0000, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > This is something I've been trying to figure out for some time.  Is
> > there a way in Python to take a string [say something from a
> > raw_input] and make that string a variable name?  I want to to this
> so
> > that I can create class instances on-the-fly, using a user-entered
> > string as the instance name.
> 
> This comes up regularly from beginners and is nearly always a bad
> idea!
> 
> The easy solution is to use a dictionary to store the instances.
> See the OOP topic in my tutor for an example using bankAccounts
> as the objects...
> 
> > could accomplish something similar using a plain old dictionary, but
> I
> > was playing around with the OOP stuff and thought it might be a neat
> > thing to try out.
> 
> A Dictionary is best. The problem is, once you create your new
> variables none of the rest of your code knows aout them so
> how can it access them. And what if someone enters a name that
> is being iused elsewhere in your code? You will overwrite a
> real variable with this new object! Very tricky to control.
> 
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to