variable name using a for

Discussion in 'Python' started by Alex, Mar 2, 2004.

  1. Alex

    Alex Guest

    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
     
    Alex, Mar 2, 2004
    #1
    1. Advertising

  2. In article <4044d7f3$0$4664$>,
    Alex <> wrote:
    >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?

    .
    .
    .
    While you can do that, you shouldn't. Unless
    there are requirements you haven't explained,
    you're better off exploiting a dictionary:

    name = {}
    for y in range(nbSujects):
    name[y] = 'abc'
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
     
    Cameron Laird, Mar 2, 2004
    #2
    1. Advertising

  3. Alex

    Alex Guest

    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 ?

    Thanks again

    Alex
     
    Alex, Mar 2, 2004
    #3
  4. Alex

    Sean Ross Guest

    "Alex" <> wrote in message
    news:4044e177$0$24960$...
    [snip]
    > so in my code:
    > for x in range(nbSujets):
    > self.button1 = xbmcgui.ControlButton(50, 100, 40, 30, "B"+str(x) )
    > self.addControl(self.button1)


    for x in range(nbSujets):
    button_name = "button%d"%x
    setattr(self, button_name, xbmcgui.ControlButton(50, 100, 40, 30,
    "B%d"%x ))
    self.addControl(getattr(self, button_name))
     
    Sean Ross, Mar 2, 2004
    #4
  5. Alex

    Mel Wilson Guest

    In article <4044e177$0$24960$>,
    "Alex" <> 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.
     
    Mel Wilson, Mar 2, 2004
    #5
  6. Alex

    Peter Otten Guest

    Alex 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)
    >


    You have several options:

    (1)
    for x in range(nbSujets):
    button = xbmcgui.ControlButton(...)
    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.ControlButton(...)
    self.addControl(button)
    self.buttons.append(button)

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

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

    Refer to the button later as self.button0, 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...


    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.killMonster, self.saveTheWorld]):
    button = xbmcgui.ControlButton(...)
    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
     
    Peter Otten, Mar 2, 2004
    #6
  7. Alex

    Alex Guest

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

    Alex

    "Sean Ross" <> a écrit dans le message de
    news:xv51c.8749$...
    >
    > "Alex" <> wrote in message
    > news:4044e177$0$24960$...
    > [snip]
    > > so in my code:
    > > for x in range(nbSujets):
    > > self.button1 = xbmcgui.ControlButton(50, 100, 40, 30, "B"+str(x) )
    > > self.addControl(self.button1)

    >
    > for x in range(nbSujets):
    > button_name = "button%d"%x
    > setattr(self, button_name, xbmcgui.ControlButton(50, 100, 40, 30,
    > "B%d"%x ))
    > self.addControl(getattr(self, button_name))
    >
    >
    >
    >
     
    Alex, Mar 2, 2004
    #7
  8. Alex

    Sean Ross Guest

    "Alex" <> wrote in message
    news:404501c1$0$24935$...
    > Sean you rock!!!!!!!!
    > You solved my problem !
    > 1000 thank you are not enough ! :)
    >
    > Alex

    [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
     
    Sean Ross, Mar 2, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    876
    Roedy Green
    Jan 2, 2006
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,426
    Joe Smith
    Apr 22, 2007
  3. mfglinux
    Replies:
    11
    Views:
    736
    Roberto Bonvallet
    Sep 12, 2007
  4. Peter Buckley
    Replies:
    5
    Views:
    219
    matt neuburg
    Feb 27, 2009
  5. David Filmer
    Replies:
    19
    Views:
    267
    Kevin Collins
    May 21, 2004
Loading...

Share This Page