Iterate creating variables?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tdahsu@gmail.com

    Iterate creating variables?

    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 "variabilit y" aspect I want.

    Is there a way?

    I appreciate any and all answers!

    Thanks!
  • Diez B. Roggisch

    #2
    Re: Iterate creating variables?

    tdahsu@gmail.co m 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 "variabilit y" aspect I want.
    >
    Is there a way?
    Keep either a list or dictionary around. Like this:

    checkboxes = []

    for o in xrange(25):
    checkboxes.appe nd(....create a checkbox...)

    self.checkboxes = checkboxes

    Diez

    Comment

    • Reedick, Andrew

      #3
      RE: Iterate creating variables?


      -----Original Message-----
      From: python-list-bounces+jr9445= att.com@python. org [mailto:python-
      list-bounces+jr9445= att.com@python. org] On Behalf Of Diez B. Roggisch
      Sent: Friday, June 13, 2008 11:21 AM
      To: python-list@python.org
      Subject: Re: Iterate creating variables?

      tdahsu@gmail.co m 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.appe nd(....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(N one) # since there is no s0, let's use 1 based list

      for i in range(1, 5):
      code = "s%d" % i
      s_list.append(e val(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


      Comment

      • Reedick, Andrew

        #4
        RE: Iterate creating variables?


        -----Original Message-----
        From: python-list-bounces+jr9445= att.com@python. org [mailto:python-
        list-bounces+jr9445= att.com@python. org] On Behalf Of tdahsu@gmail.co m
        Sent: Friday, June 13, 2008 11:11 AM
        To: python-list@python.org
        Subject: Iterate creating variables?

        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 "variabilit y" aspect I want.

        Is there a way?

        I appreciate any and all answers!

        Either store the checkboxes in an array or hash/dictionary. If that's
        not practical, then
        You can use strings to build the code and use eval to execute the string
        as code. Ex:

        for i in range(10):
        code = "%d + %d" % (i, i)
        print eval(code)



        Comment

        • tdahsu@gmail.com

          #5
          Re: Iterate creating variables?

          On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
          tda...@gmail.co m 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 "variabilit y" aspect I want.
          >
          Is there a way?
          >
          Keep either a list or dictionary around. Like this:
          >
          checkboxes = []
          >
          for o in xrange(25):
               checkboxes.appe nd(....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(sel f.panel01, 'Checkbox1')
          .
          .
          .
          self.checkbox25 = xrc.XRCCTRL(sel f.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.

          Or am I missing something?

          (I'm guessing that's likely!)

          Thanks.

          Comment

          • Diez B. Roggisch

            #6
            Re: Iterate creating variables?

            tdahsu@gmail.co m schrieb:
            On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
            >tda...@gmail.c om schrieb:
            >>
            >>
            >>
            >>I have twenty-five checkboxes I need to create (don't ask):
            >>self.checkbox 1 = ...
            >>self.checkbox 2 = ...
            >>.
            >>.
            >>.
            >>self.checkbox 25 = ...
            >>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 "variabilit y" aspect I want.
            >>Is there a way?
            >Keep either a list or dictionary around. Like this:
            >>
            >checkboxes = []
            >>
            >for o in xrange(25):
            > checkboxes.appe nd(....create a checkbox...)
            >>
            >self.checkboxe s = checkboxes
            >>
            >Diez
            >
            I don't understand... how do I then complete the assignment statement?
            >
            If I have:
            >
            self.checkbox1 = xrc.XRCCTRL(sel f.panel01, 'Checkbox1')
            .
            .
            .
            self.checkbox25 = xrc.XRCCTRL(sel f.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.appe nd(xrc.XRCCTRL( self.panel01, 'Checkbox%i' % o))

            is the way to go.

            Diez

            Comment

            • tdahsu@gmail.com

              #7
              Re: Iterate creating variables?

              On Jun 13, 11:48 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
              tda...@gmail.co m schrieb:
              >
              >
              >
              On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
              tda...@gmail.co m schrieb:
              >
              >I have twenty-five checkboxes I need to create (don't ask):
              >self.checkbo x1 = ...
              >self.checkbo x2 = ...
              >.
              >.
              >.
              >self.checkbox2 5 = ...
              >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 "variabilit y" aspect I want.
              >Is there a way?
              Keep either a list or dictionary around. Like this:
              >
              checkboxes = []
              >
              for o in xrange(25):
                   checkboxes.appe nd(....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(sel f.panel01, 'Checkbox1')
              .
              .
              .
              self.checkbox25 = xrc.XRCCTRL(sel f.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.appe nd(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()

              to determine if the box is checked or not.

              I should have included that piece in the initial problem description;
              my apologies.

              Comment

              • Diez B. Roggisch

                #8
                Re: Iterate creating variables?

                >
                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()
                >
                to determine if the box is checked or not.
                >
                I should have included that piece in the initial problem description;
                my apologies.

                Then translate the above to

                self.checkboxes[1].GetValue()

                The point of all this is the following: If you have a variable (even if
                not changing often, or being globally configured) number of objects,
                it's a good idea to keep them around in a list.

                Even if you need specific parameters for the checkboxes, it would most
                probably be better to do it like this

                checkbox_args = [
                ("name", parameter, ...),
                ("other_name ", other_parameter , ...),
                ]

                for parameters in checkbox_args:
                checkboxes.appe nd(Checbbox(*pa rameters))


                If you know on the other hand that you will have 10 checkboxes which
                have all a defined meaning, it might be better to use a meaningful name
                for them, like:


                self.create_bac kup = Checkbox(...)
                self.perform_au thorization = Checkbox(...)

                Comment

                • tdahsu@gmail.com

                  #9
                  Re: Iterate creating variables?

                  On Jun 13, 12:03 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                  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()
                  >
                  to determine if the box is checked or not.
                  >
                  I should have included that piece in the initial problem description;
                  my apologies.
                  >
                  Then translate the above to
                  >
                  self.checkboxes[1].GetValue()
                  >
                  The point of all this is the following: If you have a variable (even if
                  not changing often, or being globally configured) number of objects,
                  it's a good idea to keep them around in a list.
                  >
                  Even if you need specific parameters for the checkboxes, it would most
                  probably be better to do it like this
                  >
                  checkbox_args = [
                     ("name", parameter, ...),
                     ("other_name ", other_parameter , ...),
                  ]
                  >
                  for parameters in checkbox_args:
                      checkboxes.appe nd(Checbbox(*pa rameters))
                  >
                  If you know on the other hand that you will have 10 checkboxes which
                  have all a defined meaning, it might be better to use a meaningful name
                  for them, like:
                  >
                  self.create_bac kup = Checkbox(...)
                  self.perform_au thorization = Checkbox(...)
                  Trying self.checkboxes[1].GetValue(), gives me:

                  'NoneType' object has no attribute "GetValue"

                  Thanks.

                  Comment

                  • Calvin Spealman

                    #10
                    Re: Iterate creating variables?


                    On Jun 13, 2008, at 11:56 AM, tdahsu@gmail.co m wrote:
                    On Jun 13, 11:48 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                    >tda...@gmail.c om schrieb:
                    >>
                    >>
                    >>
                    >>On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                    >>>tda...@gmail .com schrieb:
                    >>
                    >>>>I have twenty-five checkboxes I need to create (don't ask):
                    >>>>self.checkb ox1 = ...
                    >>>>self.checkb ox2 = ...
                    >>>>.
                    >>>>.
                    >>>>.
                    >>>>self.checkb ox25 = ...
                    >>>>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 "variabilit y" aspect I want.
                    >>>>Is there a way?
                    >>>Keep either a list or dictionary around. Like this:
                    >>
                    >>>checkboxes = []
                    >>
                    >>>for o in xrange(25):
                    >>> checkboxes.appe nd(....create a checkbox...)
                    >>
                    >>>self.checkbo xes = checkboxes
                    >>
                    >>>Diez
                    >>
                    >>I don't understand... how do I then complete the assignment
                    >>statement?
                    >>
                    >>If I have:
                    >>
                    >>self.checkbox 1 = xrc.XRCCTRL(sel f.panel01, 'Checkbox1')
                    >>.
                    >>.
                    >>.
                    >>self.checkbox 25 = xrc.XRCCTRL(sel f.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.appe nd(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

                    Comment

                    • tdahsu@gmail.com

                      #11
                      Re: Iterate creating variables?

                      On Jun 13, 12:19 pm, Calvin Spealman <ironfro...@soc ialserve.com>
                      wrote:
                      On Jun 13, 2008, at 11:56 AM, tda...@gmail.co m wrote:
                      >
                      >
                      >
                      On Jun 13, 11:48 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                      tda...@gmail.co m schrieb:
                      >
                      >On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                      >>tda...@gmail. com schrieb:
                      >
                      >>>I have twenty-five checkboxes I need to create (don't ask):
                      >>>self.checkbo x1 = ...
                      >>>self.checkbo x2 = ...
                      >>>.
                      >>>.
                      >>>.
                      >>>self.checkbo x25 = ...
                      >>>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 "variabilit y" aspect I want.
                      >>>Is there a way?
                      >>Keep either a list or dictionary around. Like this:
                      >
                      >>checkboxes = []
                      >
                      >>for o in xrange(25):
                      >>     checkboxes.appe nd(....create a checkbox...)
                      >
                      >>self.checkbox es = checkboxes
                      >
                      >>Diez
                      >
                      >I don't understand... how do I then complete the assignment  
                      >statement?
                      >
                      >If I have:
                      >
                      >self.checkbo x1 = xrc.XRCCTRL(sel f.panel01, 'Checkbox1')
                      >.
                      >.
                      >.
                      >self.checkbox2 5 = xrc.XRCCTRL(sel f.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.appe nd(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.

                      Comment

                      • Jason Scheirer

                        #12
                        Re: Iterate creating variables?

                        On Jun 13, 8:11 am, tda...@gmail.co m wrote:
                        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 "variabilit y" aspect I want.
                        >
                        Is there a way?
                        >
                        I appreciate any and all answers!
                        >
                        Thanks!
                        for x in xrange(1, 26):
                        setattr(self, 'checkbox_%i' % x, ...)

                        Comment

                        • Calvin Spealman

                          #13
                          Re: Iterate creating variables?

                          That smells bad the same was the eval() usage in the thread "Hard to
                          understand 'eval'". This is only one pythoners opinion, of course.

                          On Jun 13, 2008, at 3:29 PM, Jason Scheirer wrote:
                          for x in xrange(1, 26):
                          setattr(self, 'checkbox_%i' % x, ...)
                          --
                          http://mail.python.org/mailman/listinfo/python-list

                          Comment

                          • Hyuga

                            #14
                            Re: Iterate creating variables?

                            On Jun 13, 11:34 am, "Reedick, Andrew" <jr9...@ATT.COM wrote:
                            -----Original Message-----
                            From: python-list-bounces+jr9445= att....@python. org [mailto:python-
                            list-bounces+jr9445= att....@python. org] On Behalf Of tda...@gmail.co m
                            Sent: Friday, June 13, 2008 11:11 AM
                            To: python-l...@python.org
                            Subject: Iterate creating variables?
                            >
                            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 "variabilit y" aspect I want.
                            >
                            Is there a way?
                            >
                            I appreciate any and all answers!
                            >
                            Either store the checkboxes in an array or hash/dictionary. If that's
                            not practical, then
                            You can use strings to build the code and use eval to execute the string
                            as code. Ex:
                            >
                            for i in range(10):
                            code = "%d + %d" % (i, i)
                            print eval(code)
                            Don't do this. You want

                            for idx in range(10):
                            setattr(self, 'checkbox_%i' % idx)

                            Comment

                            • Mark Tolonen

                              #15
                              Re: Iterate creating variables?


                              "Hyuga" <hyugaricdeau@g mail.comwrote in message
                              news:f9b72959-8c81-4e26-84e6-c80b1c84c4b2@34 g2000hsh.google groups.com...
                              On Jun 13, 11:34 am, "Reedick, Andrew" <jr9...@ATT.COM wrote:
                              -----Original Message-----
                              From: python-list-bounces+jr9445= att....@python. org [mailto:python-
                              list-bounces+jr9445= att....@python. org] On Behalf Of tda...@gmail.co m
                              Sent: Friday, June 13, 2008 11:11 AM
                              To: python-l...@python.org
                              Subject: Iterate creating variables?
                              >>
                              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 "variabilit y" aspect I want.
                              >>
                              Is there a way?
                              >>
                              I appreciate any and all answers!
                              >>
                              >Either store the checkboxes in an array or hash/dictionary. If that's
                              >not practical, then
                              >You can use strings to build the code and use eval to execute the string
                              >as code. Ex:
                              >>
                              >for i in range(10):
                              > code = "%d + %d" % (i, i)
                              > print eval(code)
                              >
                              Don't do this. You want
                              >
                              for idx in range(10):
                              setattr(self, 'checkbox_%i' % idx)
                              Assuming create_checkbox () is the function to create a checkbox, this will
                              create a list of 25 checkboxes::

                              checkbox = [create_checkbox () for i in xrange(25)]

                              --Mark

                              Comment

                              Working...