从带有参数的自定义文件调用函数

Call function from a custom file with parameters

如何从自定义外部文件中调用函数并能够有参数?

外部文件是 XML 文件。标签如下所示:

<menu name="bar">
        <option func="boo">Execute Me</option>
        <option func="foo" params="a, b, c, d">I want parameters</option>        
</menu>

我想执行 func 属性中定义的函数。我已经构建了解析所有内容的代码,我只需要调用它。问题是我需要从将导入另一个文件的 class 中调用它。 像这样:

 class xmlParser:
   def __init__(self, filepath, funcname, *params):
      # code to parse data etc.      
        exec_func_from_file(funcname, params)


   def exec_func_from_file(self, *args):
        # code to call function.
        ...
        ...
        funcname(params)

接下来我想要一个class/module来保存所有要执行的函数

class functions:
   def __init__(self):
       pass

   def boo(self):
       print "Well done"

   def foo(self, a, b, c, d):
     print 'Executed'

然后它将是另一个它将像这样使用 class。

 import xmlParser
 import functions

 filepath = 'files/test.xml' 

 if var1 == var2: 
   params = 'stored from somewhere' 
   xmlParser(filepath, 'foo', *params)
 else:
   xmlParser(filepath, 'boo', *params)
class xmlParser:
   def __init__(self, filepath, funcname, *args):
        # code to parse data etc.
        exec_func_from_file(funcname, args)

   def exec_func_from_file(self, funcname, *args):
        # code to call function.
        ...
        ...
        functionsobj = functions() # do you really need a class for this?
        funcobj = getattr(functionsobj, funcname)
        funcname(*args)