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/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to