On Sat, 2006-07-22 at 14:11 +0100, John CORRY wrote: > Hi, > > I am refactoring my code. I am trying to reduce the amount of lines > by using more loops. I tend to use copy and paste a lot instead of > writing a loop to do the work. > > For example, I have 30 textentry boxes numbered from entry20 to > entry50. > I have used the following code to assign the entryboxes to a local > name. > > text20 = self.wTree.get_widget("entry20") > text21 = self.wTree.get_widget("entry21") > > I have had a go at writing a loop for the above 30 textentry boxes. > It is below, but it does not work. > > for x in range(20,51): > ent = "entry%s" % (str(x)) > > text_x = self.wTree.get_widget(ent) > > Is it possible to do what I want it to do?
NO. You are looking to create local variables "on the fly". But there is a simple solution that accomplishes what you really want. > Am I on the right lines? Rather than use local variables, create a container to hold the entry values. Then access the entry values from the container. One container to consider is a dictionary. It allows you to retrieve values using fairly arbitrary keys. Rewriting your loop from above, text = {} # create dictionary for x in range(20,51): ent = "entry%s" % (x) # removed unnecessary str(x) text[x] = self.wTree.get_widget(ent) Now you can retrieve the values from text using the numbers. So rather than text_x, you will code text[x] (or text[20], etc.). > Any help or advice would be greatly appreciated. (For program snippets and the kind of content on this list, HTML often gets in the way. My email program (Evolution) rendered your email in a tiny, illegible font. Turning off HTML would be helpful.) > > Thanks, > > John. -- Lloyd Kvam Venix Corp _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor