Python通过函数名调用函数的几种场景

打印 上一主题 下一主题

主题 860|帖子 860|积分 2580

除了执行系统下令外,我们偶然还需要动态地执行一些python代码,有经验的朋友就会知道可以利用内置函数eval实现这一需求,如eval("print(__file__)"),这还是比力简单的。
但如果要动态执行一个函数,讲的资料就会少一点,这次就要看这个需求该怎样实现。
一、通过eval实现

1 通过eval调用同一个类内的函数
  1. class TestA:
  2.     def __init__(self):
  3.         self.config_dict = {
  4.             "be_called_function_name": "self.be_called_function()",
  5.         }
  6.         pass
  7.     def active_call_function(self):
  8.         print("here is active_call_function.")
  9.         be_called_function_name = self.config_dict["be_called_function_name"]
  10.         # 就直接调用。如果有其他参数,一样地传就好了
  11.         # 另外也可以是"be_called_function_name"是"be_called_function",然后eval(be_called_function_name)()
  12.         eval(be_called_function_name)
  13.         pass
  14.     def be_called_function(self):
  15.         print("here is be_called_function.")
  16. if __name__ == "__main__":
  17.     obj = TestA()
  18.     obj.active_call_function()
复制代码
2 通过eval调用同一个文件内的一级函数
  1. class TestA:
  2.     def __init__(self):
  3.         self.config_dict = {
  4.             "be_called_function_name": "be_called_function()",
  5.         }
  6.         pass
  7.     def active_call_function(self):
  8.         print("here is active_call_function.")
  9.         be_called_function_name = self.config_dict["be_called_function_name"]
  10.         # 就直接调用。如果有其他参数,一样地传就好了
  11.         # 另外也可以是"be_called_function_name"是"be_called_function",然后eval(be_called_function_name)()
  12.         eval(be_called_function_name)
  13.         pass
  14. def be_called_function():
  15.     print("here is be_called_function.")
  16. if __name__ == "__main__":
  17.     obj = TestA()
  18.     obj.active_call_function()
复制代码
二、通过getattr实现

1 通过函数名调用同一个类内的函数
  1. class TestA:
  2.     def __init__(self):
  3.         self.config_dict = {
  4.             "be_called_function_name": "be_called_function",
  5.         }
  6.         pass
  7.     def active_call_function(self):
  8.         print("here is active_call_function.")
  9.         # getaattr(module_name, function_name),module_name传self即可
  10.         be_called_function = getattr(self, self.config_dict["be_called_function_name"])
  11.         # 就直接调用。如果有其他参数,一样地传就好了
  12.         be_called_function()
  13.         pass
  14.     def be_called_function(self):
  15.         print("here is be_called_function.")
  16. if __name__ == "__main__":
  17.     obj = TestA()
  18.     obj.active_call_function()
复制代码
2 通过函数名调用其他类的函数
  1. class TestA:
  2.     def __init__(self):
  3.         self.config_dict = {
  4.             "be_called_function_name": "be_called_function",
  5.         }
  6.         pass
  7.     def active_call_function(self):
  8.         print("here is active_call_function.")
  9.         # getaattr(module_name, function_name),module_name传被调用的函数所在的类的类实例
  10.         testb_obj = TestB()
  11.         be_called_function = getattr(testb_obj, self.config_dict["be_called_function_name"])
  12.         # 就直接调用。如果有其他参数,一样地传就好了
  13.         be_called_function()
  14.         pass
  15. class TestB:
  16.     def be_called_function(self):
  17.         print("here is be_called_function.")
  18. if __name__ == "__main__":
  19.     obj = TestA()
  20.     obj.active_call_function()
复制代码
3 通过函数名调用同文件的一级函数
  1. import sys
  2. class TestA:
  3.     def __init__(self):
  4.         self.config_dict = {
  5.             "be_called_function_name": "be_called_function",
  6.         }
  7.         pass
  8.     def active_call_function(self):
  9.         print("here is active_call_function.")
  10.         # getaattr(module_name, function_name),module_name传当前模块名
  11.         module_name = sys.modules['__main__']
  12.         be_called_function = getattr(module_name, self.config_dict["be_called_function_name"])
  13.         # 就直接调用。如果有其他参数,一样地传就好了
  14.         be_called_function()
  15.         pass
  16. def be_called_function():
  17.     print("here is be_called_function.")
  18. if __name__ == "__main__":
  19.     obj = TestA()
  20.     obj.active_call_function()
复制代码
4 通过函数名调用在其他文件的一级函数
  1. class TestA:
  2.     def __init__(self):
  3.         self.config_dict = {
  4.             "be_called_function_name": "be_called_function",
  5.         }
  6.         pass
  7.         
  8.         #学习中遇到问题没人解答?小编创建了一个Python学习交流群:153708845
  9.     def active_call_function(self):
  10.         print("here is active_call_function.")
  11.         # getaattr(module_name, function_name),module_name传函数所在模块名
  12.         # __import__()传函数所在文件
  13.         module_name = __import__("test_call_function_by_string1")
  14.         be_called_function = getattr(module_name, self.config_dict["be_called_function_name"])
  15.         # 就直接调用。如果有其他参数,一样地传就好了
  16.         be_called_function()
  17.         pass
  18. if __name__ == "__main__":
  19.     obj = TestA()
  20.     obj.active_call_function()
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表