python通过函数名调用函数的几种场景
作者:诸子流 发布时间:2023-10-21 00:25:43
标签:python,函数名,调用,函数
一、说明
之前写了一篇“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()
来源:https://www.cnblogs.com/lsdb/p/13294495.html


猜你喜欢
- SQL Server服务器的配置选项属于那种人们了解较少且经常误用的选项。当一个技术支持人员要求你按照某种方式调整一个选项、而另一个技术支持
- pd.DataFrame中通常含有许多特征,有时候需要对每个含有缺失值的列,都用均值进行填充,代码实现可以这样:for column in
- 常用配置以下配置能使用File -> New Projects Settings -> Settings for New Pro
- 什么是JSON http://www.json.org/json-zh.htmlJSON(Javascript Object Notatio
- 在本身比较复杂的页面里,再突出信息,往往是把几种方法叠加起来使用,比如加粗加大、加粗加色等,区别在于使用的类别和程度。导致的结果是呈现越来越
- 实际上face_recognition这个项目尤其是dlib更适用于Linux系统。经过我的测试,在性能方面,编译同样规格的项目,这个工具在
- 首先,我们需要在vue工程中安装video.js相关依赖。npm install --save video.jsnpm install --
- 本文实例讲述了golang使用sort接口实现排序的方法。分享给大家供大家参考,具体如下:今天看见群里再讨论排序的sort.Interfac
- yaml 文件的应用场景与格式介绍yaml 文件的应用场景yaml其实也类似于 json、txt ,它们都属于一种文本格式。在我们的实际工作
- 1、什么是UUIDUUID简介通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构
- 运行cmdsqlplus/nolog 回车SQL>conn/as sysdba 回车SQL>alter user scott a
- 本文主要介绍了OpenCV 图像对比度,具有一定的参考价值,感兴趣的可以了解一下实现原理图像对比度指的是一幅图像中明暗区域最亮的白和最暗的黑
- 1.什么是标准库(Standard Library)标准库指的是js的标准内置对象,是js这门语言本身初始时提供的在全局范围的对象2.Obj
- 本文实例讲述了Python计算字符宽度的方法。分享给大家供大家参考,具体如下:最近在用python写一个CLI小程序,其中涉及到计算字符宽度
- 简介OpenCV中使用VideoCapture类写的视频是没有音频的,如果要进一步处理音频则需要用到一个库——MoviePy,这个库是Pyt
- 本文实例讲述了golang中strconv.ParseInt函数用法。分享给大家供大家参考,具体如下:golang strconv.Pars
- 本文实例讲述了js中forEach,for in,for of循环的用法。分享给大家供大家参考,具体如下:一、一般的遍历数组的方法:var
- 当Python中用到双重for循环设计的时候我一般会使用循环的嵌套,但是在Python中其实还存在另一种技巧——for复合语句。简单写一个小
- 这篇博客将介绍如何通过OpenCV和Python使用模板匹配执行光学字符识别(OCR)。具体来说,将使用Python+OpenCV实现模板匹
- python读取npy文件时,太大不能完全显示,其解决方法当用python读取npy文件时,会遇到npy文件太大,用print函数打印时不能