executing a function/method from a variable

Terry Reedy tjreedy at udel.edu
Sat Oct 17 18:46:57 EDT 2009


Yves wrote:
> What is the best way to execute a function which name is stored in a 
> variable ?

One standard way is a dict mapping names to function objects.

 >>> numfuncs = {'int':int, 'float':float}
 >>> f='int'
 >>> numfuncs[f]('33')
33

Since attributes gets mapped to a dict, this is similar to previous answer.

tjr




More information about the Python-list mailing list