On Sat, 2006-07-22 at 17:18 +0200, Karl Pflästerer wrote: > On 22 Jul 2006, [EMAIL PROTECTED] wrote: > > > > > 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. > > The "no" is not absolutely right IMO. He could write directly in the > dictionary he gets when he calls locals() (but I think you're right in > saying that this is only seldom a good idea).
Well the reference documentation says: locals( ) Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. I believe the compiler thinks it knows of all the local variables, so I think assignment to locals() is likely to lead to grief even it appears to work in simple test cases. -- Lloyd Kvam Venix Corp _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor