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 p
Carsten Haese 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.
Yes I agree, every time I use eval, I feel like the language is missing
something, fair enough to shell type languages, but usually not the cas
On Oct 17, 3:02 am, Yves 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 execute
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):
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 patte