3

i have few variables like this:

self.lamp_1
self.lamp_2
self.lamp_3
self.lamp_4

and now i want to use each of this names is loop to call them automaticly, like this:

for i in range(1,5):
    self.canvas.itemconfig(self.lamp_/number_i_automaticly/, fill=self.color_blink)

I tried using function eval() but it doesn't work. It stops running my program.

eval("self.canvas.itemconfig(self.lamp_"+str(i)+",fill=self.color_blink)")

how can i declerate names of variables in that way, using key i?

0

4 Answers 4

8

Use a list instead of indexed variable names:

self.lamps = [lamp_1, lamp_2, lamp_3, lamp_4]

If you insist on using indexed variable names (you shouldn't), you can use getattr():

for i in range(1, 5):
    self.canvas.itemconfig(getattr(self, "lamp_%i" % i), 
                           fill=self.color_blink)
Sign up to request clarification or add additional context in comments.

5 Comments

getattr() doesn't work properly. it's says that's something wrong with arguments. what could that be?
@greg: How am I supposed to know? You neither told me what you tried nor what error message you got. :)
i find out what the problem was. this is for version 2.7 and I'm using 3.2, it works fine. thanks and sorry for weak comment.
@greg: There's no difference in the usage of getattr() between 2.7 and 3.2. The above code should work in both versions.
i don't know what was the problem than, i copy your version and there was and error. important is that we solved the problem ;)
2

You can use getattr for this:

for i in range(1,5):
    self.canvas.itemconfig(getattr(self, 'lamp_%s' % i),
                           fill=self.color_blink)

But you really shouldn't name your variables like this. Put them in a list and iterate over that:

self.lamps = [self.lamp_1, self.lamp_2, self.lamp_3, self.lamp_4]
for lamp in self.lamps:
    self.canvas.itemconfig(lamp, fill=self.color_blink)

2 Comments

i used this: self.canvas.itemconfig(eval("self.lamp_{0}".format(i)), fill=self.color_blink) and it works fine! thanks a lot!!
@greg: It's a very bad idea to use indexed variable names. It's an even worse idea to use eval() instead of getattr() to access them. Do yourself a favour by not ignoring our advice! :)
2

Try to avoid using eval whenever possible. In this case you could use getattr:

for i in range(1, 5):
    value = getattr(self, 'lamp_%d' % i)

But I agree: you should be using a list (or a dictionary) instead.

Edit: oh, and use setattr to set them:

setattr(self, 'lamp_%d' % i, value)

Comments

1

I suggest doing this as @SvenMarnach suggests. However, if you must use for some external reason lamp_1 etc, you should change your loop from range() to:

for lamp in (self.lamp_1, self.lamp_2, self.lamp_3, self.lamp_4):
    self.canvas.itemconfig(lamp, fill=self.color_blink)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.