executing a function/method from a variable
yves at zioup.com
Sat Oct 17 15:17:28 CEST 2009
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 case in
python. That's why I asked,
> In this case, you
> should use getattr().
Right ! I always forget about get/set attr. Thanks !
> Here's your simplified example modified to use getattr:
Tried it, and it works if we remove the self from the name of course:
def __init__(self, arg):
self.todo = 'print' + arg;
#func = getattr(self, self.todo)
o = dummy(sys.argv)
More information about the Python-list