variable name using a for

A

Alex

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
 
C

Cameron Laird

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'
 
A

Alex

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
 
S

Sean Ross

[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))
 
M

Mel Wilson

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.
 
P

Peter Otten

Alex said:
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
 
A

Alex

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

Alex

Sean Ross said:
[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))
 
S

Sean Ross

Alex said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top