newbie question about self and eval

Hamish Lawson hamish_lawson at yahoo.co.uk
Tue Jan 25 08:55:16 EST 2000


>             token = string.split(lines,'\011')
>             bucket = token[0]
>             if bucket =='fee':
>                 self.fee.append(token[1])
>             elif bucket == 'fi':
>                 self.fi.append(token[1])
>             elif bucket == 'fo':
>                 self.fo.append(token[1])
>             else:
>                 self.fum.append(token[1])

> I know there must be a more effective way of appending items into the
> various class lists, (which probably involves eval or something
> similar

Try the following:

    token = string.split(lines,'\011')
    eval("self.%s" % token[0]).append(token[1])


Hamish Lawson


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list