executing a function/method from a variable
Yves
yves at zioup.com
Fri Oct 16 22:02:15 EDT 2009
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 pattern:
import sys
class dummy(object):
def __init__(self, arg):
self.todo = 'self.print' + arg;
def printa(self):
print 'a'
def printb(self):
print 'b'
def doit(self):
#k = eval(self.todo)
#k()
eval(self.todo)()
o = dummy(sys.argv[1])
o.doit()
Thanks.
--
Yves.
http://www.sollers.ca/
More information about the Python-list
mailing list