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
What is the best way to execute a function which name is stored in a variable ?
Right now I use an eval, but I'm wondering if there isn't a better way:
Here is a simplified example, but what I use this for is to parse a formated
text file, and execute a different method depending on the
Yves wrote:
Right now I use an eval, but I'm wondering if there isn't a better way:
There's (almost) always a better way than using eval. In this case, you
should use getattr().
Here's your simplified example modified to use getattr:
import sys
class dummy(object):
def __init__(self, arg):
On Oct 17, 3:02 am, Yves y...@zioup.com wrote:
What is the best way to execute a function which name is stored in a variable
?
Right now I use an eval, but I'm wondering if there isn't a better way:
Here is a simplified example, but what I use this for is to parse a formated
text file, and