Given a string - execute a function by the same name

Arnaud Delobelle arnodel at googlemail.com
Mon Apr 28 12:58:22 EDT 2008


python at bdurham.com writes:

> I'm parsing a simple file and given a line's keyword, would like to call
> the equivalently named function.
>
> There are 3 ways I can think to do this (other than a long if/elif
> construct):
>
> 1. eval() 
>
> 2. Convert my functions to methods and use getattr( myClass, "method" )
>
> 3. Place all my functions in dictionary and lookup the function to be
> called

4. Put all my functions in a module and use getattr(module, 'function')

-- 
Arnaud






More information about the Python-list mailing list