> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of Diez B. Roggisch
> Sent: Friday, June 13, 2008 11:21 AM
> To: python-list@python.org
> Subject: Re: Iterate creating variables?
> 
> [EMAIL PROTECTED] schrieb:
> > I have twenty-five checkboxes I need to create (don't ask):
> >
> > self.checkbox1 = ...
> > self.checkbox2 = ...
> > .
> > .
> > .
> > self.checkbox25 = ...
> >
> > Right now, my code has 25 lines in it, one for each checkbox, since
> > these are all variables.
> >
> > Is there a way to write a loop so that I can have fewer lines of
code
> > but still keep the variables?
> >

> 
> Keep either a list or dictionary around. Like this:
> 
> checkboxes = []
> 
> for o in xrange(25):
>      checkboxes.append(....create a checkbox...)
> 
> self.checkboxes = checkboxes
> 


And if you're too lazy to go back and convert the 25 checkboxes to an
array/list/dictionary, this will create a list from the existing
variable names:

s1 = 'one'
s2 = 'two'
s3 = 'three'
s4 = 'four'

s_list = []
s_list.append(None)  # since there is no s0, let's use 1 based list

for i in range(1, 5):
        code = "s%d" % i
        s_list.append(eval(code))


print s_list





*****

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA621


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to