variable name using a for

Mel Wilson mwilson at
Tue Mar 2 21:10:22 CET 2004

In article <4044e177$0$24960$626a14ce at>,
"Alex" <alx5962NOSPAN at> wrote:
>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.ControlButton(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
>So any hint to make this work ?

   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_buttons = []
    for x in range (nbSujets):
        b = xbmcgui.ControlButton (50, 100, 40, 30, "B"+str(x) )
        self.addControl (b)
        self.subject_buttons.append (b)

and access a specific button from the list after that.

        Regards.        Mel.

More information about the Python-list mailing list