[Tutor] String Substitution

richard python at keep-trying.com
Sun Jan 18 14:02:20 EST 2004


At 17:22 18/01/2004, you wrote:
>richard wrote:
> > Greetings,
> >
> > Once again I am stuck on strings. I  wish to get the
> > text entered
> > in a number of fields and pass it into a dict which
>Snip Snip..
>
>You are trying to do a string operation on something
>that isn't a string. Instead of writing:
>
>values.append(self.textCtrl%d.GetValue() % i
>
>You have to treat it as a string and then evaluate it.
>Here's what you should have written:
>
>values.append(eval('self.textCtrl%d.GetValue()' % i))
>
>That's not very readable, and eval isn't very
>efficient, so since it's just three items, you'd
>probably be better off writing (instead of the entire
>loop):
>
>values.append(self.textCtrl1.GetValue())
>values.append(self.textCtrl2.GetValue())
>values.append(self.textCtrl3.GetValue())
>
>Daniel Ehrenberg
I only gave three controls as an example in fact
there are 19 on this particular form. Others will have
any number of them. Up to now I had just just
being adding one control at a time as shown but
decided that it cannot be very good scripting to do
it this way especially with 19 controls and multiple
forms. This is why I was trying to use the loop.

So the new question is, should I be sticking with
one line per control, or if a loop is the way to go how do
I do this if using 'eval' is not efficient. (but does work).

Thanks for help so far,

Richard  




More information about the Tutor mailing list