variable name using a for

Mel Wilson mwilson at the-wire.com
Tue Mar 2 15:10:22 EST 2004


In article <4044e177$0$24960$626a14ce at news.free.fr>,
"Alex" <alx5962NOSPAN at yahoo.com> 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
>screen...
>
>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