您的位置:首页 > 编程语言 > Python开发

[Python]通过函数名字符串调用函数

2014-05-29 14:52 281 查看
可用方法整理如下:

1、

import module_name
methodToCall = getattr(module_name, 'fun_name_string')
result = methodToCall()


2、

module_instance = importlib.import_module('module_name_string')
methodToCall = getattr(module_instance,'fun_name_string')
result = methodToCall()


3、

locals()["fun_name_string"]()
globals()["fun_name_string"]()


参考资料:

[1]:http://stackoverflow.com/questions/3061/calling-a-function-from-a-string-with-the-functions-name-in-python
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: