Shed my a light :)

Duncan Booth duncan.booth at invalid.invalid
Mon Jun 2 13:20:36 CEST 2008

TheSaint <fc14301589 at> wrote:

> actions= ('print', 'sum', 'divide', 'myfunction')
> parameters=(5, 'nothing',5.63, object)
> for routines in actions:
>      routines(parameters)
> I'd like to note that actions are string or string expressions of the
> program functions or python itself, so I've in my program something
> like: 
> for nn in actions:
>        eval('cp.%s' %nn)
> Where cp is an instance.
> So I'm asking here whether exist a way that these string become
> functions inside my program, without using eval()

eval('cp.%s' %nn) is more cleanly done as: getattr(cp, nn)

In the more general case, you could just use the functions/methods directly 
instead of using their names:

actions = (cp.Print, cp.sum, cp.divide, cp.myfunction)
for nn in actions:

Duncan Booth

More information about the Python-list mailing list