Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用参数从自定义文件调用函数_Python_Xml_Xml Parsing - Fatal编程技术网

Python 使用参数从自定义文件调用函数

Python 使用参数从自定义文件调用函数,python,xml,xml-parsing,Python,Xml,Xml Parsing,如何从自定义外部文件调用函数,以及如何拥有参数 外部文件是一个XML文件。标签如下所示: 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):

如何从自定义外部文件调用函数,以及如何拥有参数

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

 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)

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

 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 functions:
   def __init__(self):
       pass

   def boo(self):
       print "Well done"

   def foo(self, a, b, c, d):
     print 'Executed'
然后它将是另一个,它将使用这样的类

 import xmlParser
 import functions

 filepath = 'files/test.xml' 

 if var1 == var2: 
   params = 'stored from somewhere' 
   xmlParser(filepath, 'foo', *params)
 else:
   xmlParser(filepath, 'boo', *params)

谢谢,从你的例子开始,我已经知道了怎么做。我真的很感谢你的帮助。干杯