variable name using a for

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    variable name using a for

    Hi
    I would like to create a variable (a string) in 'for' statement with a name
    linked to the for value.
    Like this:
    for y in range(nbSujets) :
    name + str(y) = 'abc'

    But it doesn't work with Python.... I read the official documentation but I
    was not able to find infos about this..
    How to make this works please?

    Thanks
    Alex


  • Cameron Laird

    #2
    Re: variable name using a for

    In article <4044d7f3$0$466 4$626a14ce@news .free.fr>,
    Alex <alx5962NOSPAN@ yahoo.com> wrote:[color=blue]
    >Hi
    >I would like to create a variable (a string) in 'for' statement with a name
    >linked to the for value.
    >Like this:
    >for y in range(nbSujets) :
    > name + str(y) = 'abc'
    >
    >But it doesn't work with Python.... I read the official documentation but I
    >was not able to find infos about this..
    >How to make this works please?[/color]

    Comment

    • Alex

      #3
      Re: variable name using a for

      Thank you very much for the reply!

      In fact I created a list in a variable and I have to create x numbers of
      butons, depending on how many values there's in the list.

      so in my code:
      for x in range(nbSujets) :
      self.button1 = xbmcgui.Control Button(50, 100, 40, 30, "B"+str(x) )
      self.addControl (self.button1)

      But in fact I need self.buttonX as I need different actions on each button.
      And the dictionnary here doesn't help I think...
      I may find tricks to cheat, but it would make dirty code or bad results on
      screen...

      So any hint to make this work ?

      Thanks again

      Alex





      Comment

      • Sean Ross

        #4
        Re: variable name using a for


        "Alex" <alx5962NOSPAN@ yahoo.com> wrote in message
        news:4044e177$0 $24960$626a14ce @news.free.fr.. .
        [snip][color=blue]
        > so in my code:
        > for x in range(nbSujets) :
        > self.button1 = xbmcgui.Control Button(50, 100, 40, 30, "B"+str(x) )
        > self.addControl (self.button1)[/color]

        for x in range(nbSujets) :
        button_name = "button%d"% x
        setattr(self, button_name, xbmcgui.Control Button(50, 100, 40, 30,
        "B%d"%x ))
        self.addControl (getattr(self, button_name))




        Comment

        • Peter Otten

          #5
          Re: variable name using a for

          Alex wrote:
          [color=blue]
          > Thank you very much for the reply!
          >
          > In fact I created a list in a variable and I have to create x numbers of
          > butons, depending on how many values there's in the list.
          >
          > so in my code:
          > for x in range(nbSujets) :
          > self.button1 = xbmcgui.Control Button(50, 100, 40, 30, "B"+str(x) )
          > self.addControl (self.button1)
          >[/color]

          You have several options:

          (1)
          for x in range(nbSujets) :
          button = xbmcgui.Control Button(...)
          self.addControl (button)

          I don't know that particular gui, but access to the button should be
          possible by something like self.getControl (buttonname) or
          self.controls[buttonindex] - just look it up in the docs.

          (2)
          self.buttons = []
          for x in range(nbSujets) :
          button = xbmcgui.Control Button(...)
          self.addControl (button)
          self.buttons.ap pend(button)

          Refer to the button later as self.buttons[buttonindex]

          (3)
          for x in range(nbSujets) :
          button = xbmcgui.Control Button(...)
          self.addControl (button)
          setattr(self, "button" + str(x), button)

          Refer to the button later as self.button0, self.button1, ...

          [color=blue]
          > But in fact I need self.buttonX as I need different actions on each
          > button. And the dictionnary here doesn't help I think...[/color]

          Perhaps you can find a way to connect buttons and actions directly in the
          for loop and need not refer to them later at all?

          (4)
          for x, action in enumerate([self.killMonste r, self.saveTheWor ld]):
          button = xbmcgui.Control Button(...)
          button.onClick = action # speculating again here
          self.addControl (button)

          killMonster() and saveTheWorld() are the methods that shall be triggered
          when the associated button is pressed.

          Incidentally, the last one is my personal favourite.

          Peter

          Comment

          • Mel Wilson

            #6
            Re: variable name using a for

            In article <4044e177$0$249 60$626a14ce@new s.free.fr>,
            "Alex" <alx5962NOSPAN@ yahoo.com> wrote:[color=blue]
            >Thank you very much for the reply!
            >
            >In fact I created a list in a variable and I have to create x numbers of
            >butons, depending on how many values there's in the list.
            >
            >so in my code:
            >for x in range(nbSujets) :
            > self.button1 = xbmcgui.Control Button(50, 100, 40, 30, "B"+str(x) )
            > self.addControl (self.button1)
            >
            >But in fact I need self.buttonX as I need different actions on each button.
            >And the dictionnary here doesn't help I think...
            >I may find tricks to cheat, but it would make dirty code or bad results on
            >screen...
            >
            >So any hint to make this work ?[/color]

            I've always been puzzled by these schemes. Sure, you can
            do these assignments, if you try hard enough, but then you
            also need other source code in your program that's aware of
            the new names. How do you plan to refer to these buttons in
            the rest of your program?

            If it were my code it would be

            self.subject_bu ttons = []
            for x in range (nbSujets):
            b = xbmcgui.Control Button (50, 100, 40, 30, "B"+str(x) )
            self.addControl (b)
            self.subject_bu ttons.append (b)

            and access a specific button from the list after that.


            Regards. Mel.

            Comment

            • Alex

              #7
              Re: variable name using a for

              Sean you rock!!!!!!!!
              You solved my problem !
              1000 thank you are not enough ! :)

              Alex

              "Sean Ross" <sross@connectm ail.carleton.ca > a écrit dans le message de
              news:xv51c.8749 $qA2.552179@new s20.bellglobal. com...[color=blue]
              >
              > "Alex" <alx5962NOSPAN@ yahoo.com> wrote in message
              > news:4044e177$0 $24960$626a14ce @news.free.fr.. .
              > [snip][color=green]
              > > so in my code:
              > > for x in range(nbSujets) :
              > > self.button1 = xbmcgui.Control Button(50, 100, 40, 30, "B"+str(x) )
              > > self.addControl (self.button1)[/color]
              >
              > for x in range(nbSujets) :
              > button_name = "button%d"% x
              > setattr(self, button_name, xbmcgui.Control Button(50, 100, 40, 30,
              > "B%d"%x ))
              > self.addControl (getattr(self, button_name))
              >
              >
              >
              >[/color]


              Comment

              • Sean Ross

                #8
                Re: variable name using a for

                "Alex" <alx5962NOSPAN@ yahoo.com> wrote in message
                news:404501c1$0 $24935$626a14ce @news.free.fr.. .[color=blue]
                > Sean you rock!!!!!!!!
                > You solved my problem !
                > 1000 thank you are not enough ! :)
                >
                > Alex[/color]
                [snip]

                I'm glad it was helpful. Still, I'd like to suggest you consider Mel
                Wilson's and Peter Otten's suggestions (in particular Peter's option (4)),
                to see whether they may better serve your purpose. I just tried to solve the
                problem you presented in the way it apeared you wanted to see it solved -
                they've suggested some design insight that may prove more beneficial.

                Best of luck,
                Sean


                Comment

                Working...