[Tutor] String Substitution

Karl Pflästerer sigurd at 12move.de
Sun Jan 18 18:51:19 EST 2004


On 18 Jan 2004, richard <- python at keep-trying.com wrote:

> 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,

What about putting the names of the functions in a list or a dictionary?

class Test(object):
    def __init__(self):
        self.funs = [eval('self.a' + str(f)) for f in xrange(1,5)]
        
    def a1(self): return 1
    def a2(self): return 2
    def a3(self): return 3
    def a4(self): return 4

    def values(self):
        vals = []
        for fun in self.funs:
            vals.append(fun())
        return vals

Here you call eval only once; that shouldn't be a bottleneck (but if you
could easily write the functions yourself).

I don't know if you wanted to store the results in the list (the list
being an attribute of the class) or just return that list, the way I did
it here.


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list