By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Iterate creating variables?

P: n/a
I have twenty-five checkboxes I need to create (don't ask):

self.checkbox1 = ...
self.checkbox2 = ...
..
..
..
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I've tried:

for o in xrange(25):
self.checkbox[o] = ...

which didn't work, and

for o in xrange(25):
self.checkbox[''%d'%(o)] = ...

which also didn't work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.

Is there a way?

I appreciate any and all answers!

Thanks!
Jun 27 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
td****@gmail.com schrieb:
I have twenty-five checkboxes I need to create (don't ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I've tried:

for o in xrange(25):
self.checkbox[o] = ...

which didn't work, and

for o in xrange(25):
self.checkbox[''%d'%(o)] = ...

which also didn't work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.

Is there a way?
Keep either a list or dictionary around. Like this:

checkboxes = []

for o in xrange(25):
checkboxes.append(....create a checkbox...)

self.checkboxes = checkboxes

Diez
Jun 27 '08 #2

P: n/a

-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of Diez B. Roggisch
Sent: Friday, June 13, 2008 11:21 AM
To: py*********@python.org
Subject: Re: Iterate creating variables?

td****@gmail.com schrieb:
I have twenty-five checkboxes I need to create (don't ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of
code
but still keep the variables?

Keep either a list or dictionary around. Like this:

checkboxes = []

for o in xrange(25):
checkboxes.append(....create a checkbox...)

self.checkboxes = checkboxes

And if you're too lazy to go back and convert the 25 checkboxes to an
array/list/dictionary, this will create a list from the existing
variable names:

s1 = 'one'
s2 = 'two'
s3 = 'three'
s4 = 'four'

s_list = []
s_list.append(None) # since there is no s0, let's use 1 based list

for i in range(1, 5):
code = "s%d" % i
s_list.append(eval(code))
print s_list

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621
Jun 27 '08 #3

P: n/a

-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of td****@gmail.com
Sent: Friday, June 13, 2008 11:11 AM
To: py*********@python.org
Subject: Iterate creating variables?

I have twenty-five checkboxes I need to create (don't ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I've tried:

for o in xrange(25):
self.checkbox[o] = ...

which didn't work, and

for o in xrange(25):
self.checkbox[''%d'%(o)] = ...

which also didn't work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.

Is there a way?

I appreciate any and all answers!

Either store the checkboxes in an array or hash/dictionary. If that's
not practical, then
You can use strings to build the code and use eval to execute the string
as code. Ex:

for i in range(10):
code = "%d + %d" % (i, i)
print eval(code)

Jun 27 '08 #4

P: n/a
On Jun 13, 11:21*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
tda...@gmail.com schrieb:
I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.
Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?
I've tried:
for o in xrange(25):
* * self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
* * self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.
Is there a way?

Keep either a list or dictionary around. Like this:

checkboxes = []

for o in xrange(25):
* * *checkboxes.append(....create a checkbox...)

self.checkboxes = checkboxes

Diez
I don't understand... how do I then complete the assignment statement?

If I have:

self.checkbox1 = xrc.XRCCTRL(self.panel01, 'Checkbox1')
.
.
.
self.checkbox25 = xrc.XRCCTRL(self.panel01, 'Checkbox25')

using your method, wouldn't I still need to figure out my original
question?

If I have a list of checkboxes, then I'll have:

checkboxes = [checkbox1, checkbox2 ... checkbox25]

in which case I'd still need to figure out how to get the variable at
the end of checkbox to do the rest of the "=" statement.

Or am I missing something?

(I'm guessing that's likely!)

Thanks.
Jun 27 '08 #5

P: n/a
td****@gmail.com schrieb:
On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>tda...@gmail.com schrieb:
>>I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.
Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?
I've tried:
for o in xrange(25):
self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.
Is there a way?
Keep either a list or dictionary around. Like this:

checkboxes = []

for o in xrange(25):
checkboxes.append(....create a checkbox...)

self.checkboxes = checkboxes

Diez

I don't understand... how do I then complete the assignment statement?

If I have:

self.checkbox1 = xrc.XRCCTRL(self.panel01, 'Checkbox1')
.
.
.
self.checkbox25 = xrc.XRCCTRL(self.panel01, 'Checkbox25')

using your method, wouldn't I still need to figure out my original
question?

If I have a list of checkboxes, then I'll have:

checkboxes = [checkbox1, checkbox2 ... checkbox25]

in which case I'd still need to figure out how to get the variable at
the end of checkbox to do the rest of the "=" statement.
I don't fully understand that. But if your code is uniform and looks
like the above, it appears that

for o in xrange(25):
checkboxes.append(xrc.XRCCTRL(self.panel01, 'Checkbox%i' % o))

is the way to go.

Diez
Jun 27 '08 #6

P: n/a
On Jun 13, 11:48*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
tda...@gmail.com schrieb:
On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
tda...@gmail.com schrieb:
>I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.
Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?
I've tried:
for o in xrange(25):
* * self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
* * self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.
Is there a way?
Keep either a list or dictionary around. Like this:
checkboxes = []
for o in xrange(25):
* * *checkboxes.append(....create a checkbox...)
self.checkboxes = checkboxes
Diez
I don't understand... how do I then complete the assignment statement?
If I have:
self.checkbox1 = xrc.XRCCTRL(self.panel01, 'Checkbox1')
.
.
.
self.checkbox25 = xrc.XRCCTRL(self.panel01, 'Checkbox25')
using your method, wouldn't I still need to figure out my original
question?
If I have a list of checkboxes, then I'll have:
checkboxes = [checkbox1, checkbox2 ... checkbox25]
in which case I'd still need to figure out how to get the variable at
the end of checkbox to do the rest of the "=" statement.

I don't fully understand that. But if your code is uniform and looks
like the above, it appears that

for o in xrange(25):
* * *checkboxes.append(xrc.XRCCTRL(self.panel01, 'Checkbox%i' % o))

is the way to go.

Diez
Thank you, this is much closer to where I need to be...

The issue is (and this is the part that you don't know, because I
didn't tell you!) is that I later need to call methods on
"self.checkbox1", for instance:

self.checkbox1.GetValue()

to determine if the box is checked or not.

I should have included that piece in the initial problem description;
my apologies.
Jun 27 '08 #7

P: n/a
>
Thank you, this is much closer to where I need to be...

The issue is (and this is the part that you don't know, because I
didn't tell you!) is that I later need to call methods on
"self.checkbox1", for instance:

self.checkbox1.GetValue()

to determine if the box is checked or not.

I should have included that piece in the initial problem description;
my apologies.

Then translate the above to

self.checkboxes[1].GetValue()

The point of all this is the following: If you have a variable (even if
not changing often, or being globally configured) number of objects,
it's a good idea to keep them around in a list.

Even if you need specific parameters for the checkboxes, it would most
probably be better to do it like this

checkbox_args = [
("name", parameter, ...),
("other_name", other_parameter, ...),
]

for parameters in checkbox_args:
checkboxes.append(Checbbox(*parameters))
If you know on the other hand that you will have 10 checkboxes which
have all a defined meaning, it might be better to use a meaningful name
for them, like:
self.create_backup = Checkbox(...)
self.perform_authorization = Checkbox(...)

Jun 27 '08 #8

P: n/a
On Jun 13, 12:03*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Thank you, this is much closer to where I need to be...
The issue is (and this is the part that you don't know, because I
didn't tell you!) is that I later need to call methods on
"self.checkbox1", for instance:
self.checkbox1.GetValue()
to determine if the box is checked or not.
I should have included that piece in the initial problem description;
my apologies.

Then translate the above to

self.checkboxes[1].GetValue()

The point of all this is the following: If you have a variable (even if
not changing often, or being globally configured) number of objects,
it's a good idea to keep them around in a list.

Even if you need specific parameters for the checkboxes, it would most
probably be better to do it like this

checkbox_args = [
* *("name", parameter, ...),
* *("other_name", other_parameter, ...),
]

for parameters in checkbox_args:
* * checkboxes.append(Checbbox(*parameters))

If you know on the other hand that you will have 10 checkboxes which
have all a defined meaning, it might be better to use a meaningful name
for them, like:

self.create_backup = Checkbox(...)
self.perform_authorization = Checkbox(...)
Trying self.checkboxes[1].GetValue(), gives me:

'NoneType' object has no attribute "GetValue"

Thanks.
Jun 27 '08 #9

P: n/a

On Jun 13, 2008, at 11:56 AM, td****@gmail.com wrote:
On Jun 13, 11:48 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>tda...@gmail.com schrieb:
>>On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
tda...@gmail.com schrieb:
>>>>I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox,
since
these are all variables.
Is there a way to write a loop so that I can have fewer lines
of code
but still keep the variables?
I've tried:
for o in xrange(25):
self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not
keeping
the "variability" aspect I want.
Is there a way?
Keep either a list or dictionary around. Like this:
>>>checkboxes = []
>>>for o in xrange(25):
checkboxes.append(....create a checkbox...)
>>>self.checkboxes = checkboxes
>>>Diez
>>I don't understand... how do I then complete the assignment
statement?
>>If I have:
>>self.checkbox1 = xrc.XRCCTRL(self.panel01, 'Checkbox1')
.
.
.
self.checkbox25 = xrc.XRCCTRL(self.panel01, 'Checkbox25')
>>using your method, wouldn't I still need to figure out my original
question?
>>If I have a list of checkboxes, then I'll have:
>>checkboxes = [checkbox1, checkbox2 ... checkbox25]
>>in which case I'd still need to figure out how to get the
variable at
the end of checkbox to do the rest of the "=" statement.

I don't fully understand that. But if your code is uniform and looks
like the above, it appears that

for o in xrange(25):
checkboxes.append(xrc.XRCCTRL(self.panel01, 'Checkbox%i' % o))

is the way to go.

Diez

Thank you, this is much closer to where I need to be...

The issue is (and this is the part that you don't know, because I
didn't tell you!) is that I later need to call methods on
"self.checkbox1", for instance:

self.checkbox1.GetValue()
self.checkbox[1].GetValue() is only two more characters and more
readable, because it expresses that you have this list of checkboxes,
where as "self.checkbox1" could be an odd name and all on its own, no
others at all, etc.

Variable variable names are a good thing to avoid. Thats why we have
containers.
>
to determine if the box is checked or not.

I should have included that piece in the initial problem description;
my apologies.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #10

P: n/a
On Jun 13, 12:19*pm, Calvin Spealman <ironfro...@socialserve.com>
wrote:
On Jun 13, 2008, at 11:56 AM, tda...@gmail.com wrote:
On Jun 13, 11:48 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
tda...@gmail.com schrieb:
>On Jun 13, 11:21 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
tda...@gmail.com schrieb:
>>>I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox, *
since
these are all variables.
Is there a way to write a loop so that I can have fewer lines *
of code
but still keep the variables?
I've tried:
for o in xrange(25):
* * self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
* * self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not *
keeping
the "variability" aspect I want.
Is there a way?
Keep either a list or dictionary around. Like this:
>>checkboxes = []
>>for o in xrange(25):
* * *checkboxes.append(....create a checkbox...)
>>self.checkboxes = checkboxes
>>Diez
>I don't understand... how do I then complete the assignment *
statement?
>If I have:
>self.checkbox1 = xrc.XRCCTRL(self.panel01, 'Checkbox1')
.
.
.
self.checkbox25 = xrc.XRCCTRL(self.panel01, 'Checkbox25')
>using your method, wouldn't I still need to figure out my original
question?
>If I have a list of checkboxes, then I'll have:
>checkboxes = [checkbox1, checkbox2 ... checkbox25]
>in which case I'd still need to figure out how to get the *
variable at
the end of checkbox to do the rest of the "=" statement.
I don't fully understand that. But if your code is uniform and looks
like the above, it appears that
for o in xrange(25):
* * *checkboxes.append(xrc.XRCCTRL(self.panel01, 'Checkbox%i' % o))
is the way to go.
Diez
Thank you, this is much closer to where I need to be...
The issue is (and this is the part that you don't know, because I
didn't tell you!) is that I later need to call methods on
"self.checkbox1", for instance:
self.checkbox1.GetValue()

self.checkbox[1].GetValue() is only two more characters and more *
readable, because it expresses that you have this list of checkboxes, *
where as "self.checkbox1" could be an odd name and all on its own, no *
others at all, etc.

Variable variable names are a good thing to avoid. Thats why we have *
containers.
to determine if the box is checked or not.
I should have included that piece in the initial problem description;
my apologies.
--
http://mail.python.org/mailman/listinfo/python-list

I don't think I'm being clear enough. Thanks to everyone for their
help.
Jun 27 '08 #11

P: n/a
On Jun 13, 8:11*am, tda...@gmail.com wrote:
I have twenty-five checkboxes I need to create (don't ask):

self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...

Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.

Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?

I've tried:

for o in xrange(25):
* * self.checkbox[o] = ...

which didn't work, and

for o in xrange(25):
* * self.checkbox[''%d'%(o)] = ...

which also didn't work.

Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.

Is there a way?

I appreciate any and all answers!

Thanks!
for x in xrange(1, 26):
setattr(self, 'checkbox_%i' % x, ...)
Jun 27 '08 #12

P: n/a
That smells bad the same was the eval() usage in the thread "Hard to
understand 'eval'". This is only one pythoners opinion, of course.

On Jun 13, 2008, at 3:29 PM, Jason Scheirer wrote:
for x in xrange(1, 26):
setattr(self, 'checkbox_%i' % x, ...)
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #13

P: n/a
On Jun 13, 11:34 am, "Reedick, Andrew" <jr9...@ATT.COMwrote:
-----Original Message-----
From: python-list-bounces+jr9445=att....@python.org [mailto:python-
list-bounces+jr9445=att....@python.org] On Behalf Of tda...@gmail.com
Sent: Friday, June 13, 2008 11:11 AM
To: python-l...@python.org
Subject: Iterate creating variables?
I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.
Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?
I've tried:
for o in xrange(25):
self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.
Is there a way?
I appreciate any and all answers!

Either store the checkboxes in an array or hash/dictionary. If that's
not practical, then
You can use strings to build the code and use eval to execute the string
as code. Ex:

for i in range(10):
code = "%d + %d" % (i, i)
print eval(code)
Don't do this. You want

for idx in range(10):
setattr(self, 'checkbox_%i' % idx)
Jun 27 '08 #14

P: n/a

"Hyuga" <hy**********@gmail.comwrote in message
news:f9**********************************@34g2000h sh.googlegroups.com...
On Jun 13, 11:34 am, "Reedick, Andrew" <jr9...@ATT.COMwrote:
-----Original Message-----
From: python-list-bounces+jr9445=att....@python.org [mailto:python-
list-bounces+jr9445=att....@python.org] On Behalf Of tda...@gmail.com
Sent: Friday, June 13, 2008 11:11 AM
To: python-l...@python.org
Subject: Iterate creating variables?
I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
.
.
.
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each checkbox, since
these are all variables.
Is there a way to write a loop so that I can have fewer lines of code
but still keep the variables?
I've tried:
for o in xrange(25):
self.checkbox[o] = ...
which didn't work, and
for o in xrange(25):
self.checkbox[''%d'%(o)] = ...
which also didn't work.
Both give the error message: "Attribute error: Main.App has no
attribute "checkbox"", which clearly indicates that I'm not keeping
the "variability" aspect I want.
Is there a way?
I appreciate any and all answers!

Either store the checkboxes in an array or hash/dictionary. If that's
not practical, then
You can use strings to build the code and use eval to execute the string
as code. Ex:

for i in range(10):
code = "%d + %d" % (i, i)
print eval(code)

Don't do this. You want

for idx in range(10):
setattr(self, 'checkbox_%i' % idx)
Assuming create_checkbox() is the function to create a checkbox, this will
create a list of 25 checkboxes::

checkbox = [create_checkbox() for i in xrange(25)]

--Mark

Jun 27 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.