Iterate creating variables?

tdahsu at gmail.com tdahsu at gmail.com
Fri Jun 13 18:29:01 CEST 2008


On Jun 13, 12:19 pm, Calvin Spealman <ironfro... at socialserve.com>
wrote:
> On Jun 13, 2008, at 11:56 AM, tda... at gmail.com wrote:
>
>
>
> > On Jun 13, 11:48 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> >> tda... at gmail.com schrieb:
>
> >>> On Jun 13, 11:21 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> >>>> tda... at gmail.com 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?
> >>>>> I've tried:
> >>>>> for o in xrange(25):
> >>>>>     self.checkbox[o] = ...
> >>>>> which didn't work, and
> >>>>> for o in xrange(25):
> >>>>>     self.checkbox[''%d'%(o)] = ...
> >>>>> which also didn't work.
> >>>>> Both give the error message: "Attribute error: Main.App has no
> >>>>> attribute "checkbox"", which clearly indicates that I'm not  
> >>>>> keeping
> >>>>> the "variability" aspect I want.
> >>>>> Is there a way?
> >>>> Keep either a list or dictionary around. Like this:
>
> >>>> checkboxes = []
>
> >>>> for o in xrange(25):
> >>>>      checkboxes.append(....create a checkbox...)
>
> >>>> self.checkboxes = checkboxes
>
> >>>> Diez
>
> >>> I don't understand... how do I then complete the assignment  
> >>> statement?
>
> >>> If I have:
>
> >>> self.checkbox1 = xrc.XRCCTRL(self.panel01, 'Checkbox1')
> >>> .
> >>> .
> >>> .
> >>> self.checkbox25 = xrc.XRCCTRL(self.panel01, 'Checkbox25')
>
> >>> using your method, wouldn't I still need to figure out my original
> >>> question?
>
> >>> If I have a list of checkboxes, then I'll have:
>
> >>> checkboxes = [checkbox1, checkbox2 ... checkbox25]
>
> >>> in which case I'd still need to figure out how to get the  
> >>> variable at
> >>> the end of checkbox to do the rest of the "=" statement.
>
> >> I don't fully understand that. But if your code is uniform and looks
> >> like the above, it appears that
>
> >> for o in xrange(25):
> >>      checkboxes.append(xrc.XRCCTRL(self.panel01, 'Checkbox%i' % o))
>
> >> is the way to go.
>
> >> Diez
>
> > Thank you, this is much closer to where I need to be...
>
> > The issue is (and this is the part that you don't know, because I
> > didn't tell you!) is that I later need to call methods on
> > "self.checkbox1", for instance:
>
> > self.checkbox1.GetValue()
>
> self.checkbox[1].GetValue() is only two more characters and more  
> readable, because it expresses that you have this list of checkboxes,  
> where as "self.checkbox1" could be an odd name and all on its own, no  
> others at all, etc.
>
> Variable variable names are a good thing to avoid. Thats why we have  
> containers.
>
>
>
> > to determine if the box is checked or not.
>
> > I should have included that piece in the initial problem description;
> > my apologies.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>

I don't think I'm being clear enough.  Thanks to everyone for their
help.



More information about the Python-list mailing list