1

I've got some problem with syntax in python.

Below is a simple example:

Instead that (1):

value = [] value.append(int(self.val1.GetValue())) value.append(int(self.val2.GetValue())) value.append(int(self.val3.GetValue())) value.append(int(self.val4.GetValue())) 

I want do sth like that (2):

value = [] for i in range(4): value.append(int(self.val + (i + 1) + .GetValue())) 

But as you see it's an invalid syntax.

The question is what I must correct in above loop to obtain the same resault like in (1)

2
  • 7
    sounds like a bad design. why don't you store the values in self as a list? Commented Mar 31, 2014 at 8:14
  • maybe its possible to store val* objects in a list? Commented Mar 31, 2014 at 8:15

1 Answer 1

2
value = [int(v.GetValue()) for v in (self.val1, self.val2, self.val3, self.val4)] 

would probably be the cleanest way.

Even shorter would be to use something with getattr(self, 'val%d' % i).GetValue(), but that's quite ugly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.