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

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

2014-08-20 19:20 183 查看
可用方法整理如下:

1、

[python]
view plaincopyprint?





import module_name 

methodToCall = getattr(module_name, 'fun_name_string') 

result = methodToCall() 

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


2、

[python]
view plaincopyprint?





module_instance = importlib.import_module('module_name_string')     

methodToCall = getattr(module_instance,'fun_name_string') 

result = methodToCall() 

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


3、

[python]
view plaincopyprint?





locals()["fun_name_string"]() 

globals()["fun_name_string"]() 

locals()["fun_name_string"]()
globals()["fun_name_string"]()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: