一、说明

之前写了一篇“Python执行系统命令教程”讲了如何执行系统命令。

除了执行系统命令外,我们有时还需要动态地执行一些python代码,有经验的朋友就会知道可以使用内置函数eval实现这一需求,如eval("print(__file__)"),这还是比较简单的。

但如果要动态执行一个函数,讲的资料就会少一点,这次就要看这个需求该如何实现。

二、通过eval实现

2.1 通过eval调用同一个类内的函数

class TestA:
 def __init__(self):
  self.config_dict = {
   "be_called_function_name": "self.be_called_function()",
  }
  pass

 def active_call_function(self):
  print("here is active_call_function.")
  be_called_function_name = self.config_dict["be_called_function_name"]
  # 就直接调用。如果有其他参数,一样地传就好了
  # 另外也可以是"be_called_function_name"是"be_called_function",然后eval(be_called_function_name)()
  eval(be_called_function_name)
  pass

 def be_called_function(self):
  print("here is be_called_function.")

if __name__ == "__main__":
 obj = TestA()
 obj.active_call_function()

2.2 通过eval调用同一个文件内的一级函数

class TestA:
 def __init__(self):
  self.config_dict = {
   "be_called_function_name": "be_called_function()",
  }
  pass

 def active_call_function(self):
  print("here is active_call_function.")
  be_called_function_name = self.config_dict["be_called_function_name"]
  # 就直接调用。如果有其他参数,一样地传就好了
  # 另外也可以是"be_called_function_name"是"be_called_function",然后eval(be_called_function_name)()
  eval(be_called_function_name)
  pass

def be_called_function():
 print("here is be_called_function.")

if __name__ == "__main__":
 obj = TestA()
 obj.active_call_function()

三、通过getattr实现

3.1 通过函数名调用同一个类内的函数

class TestA:
 def __init__(self):
  self.config_dict = {
   "be_called_function_name": "be_called_function",
  }
  pass

 def active_call_function(self):
  print("here is active_call_function.")
  # getaattr(module_name, function_name),module_name传self即可
  be_called_function = getattr(self, self.config_dict["be_called_function_name"])
  # 就直接调用。如果有其他参数,一样地传就好了
  be_called_function()
  pass

 def be_called_function(self):
  print("here is be_called_function.")


if __name__ == "__main__":
 obj = TestA()
 obj.active_call_function()

3.2 通过函数名调用其他类的函数

class TestA:
 def __init__(self):
  self.config_dict = {
   "be_called_function_name": "be_called_function",
  }
  pass

 def active_call_function(self):
  print("here is active_call_function.")
  # getaattr(module_name, function_name),module_name传被调用的函数所在的类的类实例
  testb_obj = TestB()
  be_called_function = getattr(testb_obj, self.config_dict["be_called_function_name"])
  # 就直接调用。如果有其他参数,一样地传就好了
  be_called_function()
  pass


class TestB:
 def be_called_function(self):
  print("here is be_called_function.")


if __name__ == "__main__":
 obj = TestA()
 obj.active_call_function()

3.3 通过函数名调用同文件的一级函数

import sys


class TestA:
 def __init__(self):
  self.config_dict = {
   "be_called_function_name": "be_called_function",
  }
  pass

 def active_call_function(self):
  print("here is active_call_function.")
  # getaattr(module_name, function_name),module_name传当前模块名
  module_name = sys.modules['__main__']
  be_called_function = getattr(module_name, self.config_dict["be_called_function_name"])
  # 就直接调用。如果有其他参数,一样地传就好了
  be_called_function()
  pass


def be_called_function():
 print("here is be_called_function.")


if __name__ == "__main__":
 obj = TestA()
 obj.active_call_function()

3.4 通过函数名调用在其他文件的一级函数

class TestA:
 def __init__(self):
  self.config_dict = {
   "be_called_function_name": "be_called_function",
  }
  pass

 def active_call_function(self):
  print("here is active_call_function.")
  # getaattr(module_name, function_name),module_name传函数所在模块名
  # __import__()传函数所在文件
  module_name = __import__("test_call_function_by_string1")
  be_called_function = getattr(module_name, self.config_dict["be_called_function_name"])
  # 就直接调用。如果有其他参数,一样地传就好了
  be_called_function()
  pass


if __name__ == "__main__":
 obj = TestA()
 obj.active_call_function()

以上就是python通过函数名调用函数的几种场景的详细内容,更多关于python通过函数名调用函数的资料请关注其它相关文章!

标签:
python,调用函数,python,函数名调用函数,python,函数名

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“python通过函数名调用函数的几种场景”

暂无“python通过函数名调用函数的几种场景”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。