473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 18 '05 #1
7 1030
In article <40**********************@news.free.fr>,
Alex <al***********@yahoo.com> 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?

Jul 18 '05 #2
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

Jul 18 '05 #3

"Alex" <al***********@yahoo.com> wrote in message
news:40***********************@news.free.fr...
[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))


Jul 18 '05 #4
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

Jul 18 '05 #5
In article <40***********************@news.free.fr>,
"Alex" <al***********@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.
Jul 18 '05 #6
Sean you rock!!!!!!!!
You solved my problem !
1000 thank you are not enough ! :)

Alex

"Sean Ross" <sr***@connectmail.carleton.ca> a écrit dans le message de
news:xv*******************@news20.bellglobal.com.. .

"Alex" <al***********@yahoo.com> wrote in message
news:40***********************@news.free.fr...
[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))

Jul 18 '05 #7
"Alex" <al***********@yahoo.com> wrote in message
news:40***********************@news.free.fr...
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
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: 张韡武 | last post by:
We have preffered language set as variable in xslt: <xsl:variable name="preferred_language"> zh </xsl:variable> Data: <name xml:lang="de">Raw Materials (Mining incl.)</name> <name...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.