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


Posted in Python onSeptember 23, 2020

一、说明

之前写了一篇“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 + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
Jul 14 Python
Python与shell的3种交互方式介绍
Apr 11 Python
python计算时间差的方法
May 20 Python
使用Python对MySQL数据操作
Apr 06 Python
Python有序字典简单实现方法示例
Sep 28 Python
selenium python浏览器多窗口处理代码示例
Jan 15 Python
详解python分布式进程
Oct 08 Python
python3 实现验证码图片切割的方法
Dec 07 Python
Python实现个人微信号自动监控告警的示例
Jul 03 Python
Python编译成.so文件进行加密后调用的实现
Dec 23 Python
python读写文件write和flush的实现方式
Feb 21 Python
如何用用Python将地址标记在地图上
Feb 07 Python
Python如何执行系统命令
Sep 23 #Python
Python SMTP发送电子邮件的示例
Sep 23 #Python
python两个list[]相加的实现方法
Sep 23 #Python
python matplotlib库的基本使用
Sep 23 #Python
15个应该掌握的Jupyter Notebook使用技巧(小结)
Sep 23 #Python
Python读取多列数据以及用matplotlib制作图表方法实例
Sep 23 #Python
PyCharm 2020.2下配置Anaconda环境的方法步骤
Sep 23 #Python
You might like
自己动手,丰衣足食 - 短波框形天线制作
2021/03/01 无线电
php打开远程文件的方法和风险及解决方法
2013/11/12 PHP
js call方法详细介绍(js 的继承)
2013/11/18 Javascript
javascript中数组的多种定义方法和常用函数简介
2014/05/09 Javascript
jQuery中removeData()方法用法实例
2014/12/27 Javascript
jquery插件格式实例分析
2016/06/16 Javascript
js实现浏览器倒计时跳转页面效果
2016/08/12 Javascript
AngularJS使用ng-repeat和ng-if实现数据的删选显示效果示例【适用于表单数据的显示】
2016/12/13 Javascript
svg动画之动态描边效果
2017/02/22 Javascript
深入理解JavaScript创建对象的多种方式以及优缺点
2017/06/01 Javascript
JavaScript简单实现合并两个Json对象的方法示例
2017/10/16 Javascript
在vue+element ui框架里实现lodash的debounce防抖
2019/11/13 Javascript
原生JS实现萤火虫效果
2020/03/07 Javascript
[36:20]KG vs SECRET 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
在Python中操作时间之mktime()方法的使用教程
2015/05/22 Python
Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例
2018/01/29 Python
python中(str,list,tuple)基础知识汇总
2018/02/20 Python
python的schedule定时任务模块二次封装方法
2019/02/19 Python
python对象与json相互转换的方法
2019/05/07 Python
Python面向对象编程基础实例分析
2020/01/17 Python
pycharm如何实现跨目录调用文件
2020/02/28 Python
python 已知一个字符,在一个list中找出近似值或相似值实现模糊匹配
2020/02/29 Python
记一次python 爬虫爬取深圳租房信息的过程及遇到的问题
2020/11/24 Python
法国时尚品牌乐都特瑞士站:La Redoute瑞士
2016/09/05 全球购物
墨尔本照明批发商店:Mica Lighting
2017/12/28 全球购物
酒店管理专业学生求职信
2013/09/27 职场文书
英语自荐信常用语句
2013/12/13 职场文书
五年级科学教学反思
2014/02/05 职场文书
校庆活动方案
2014/03/31 职场文书
行政执法作风整顿剖析材料
2014/10/11 职场文书
大学生旷课检讨书1000字
2015/02/19 职场文书
学术会议开幕词
2016/03/03 职场文书
会议主持词通用版
2019/04/02 职场文书
Canvas绘制像素风图片的示例代码
2021/09/25 HTML / CSS
关于Oracle12C默认用户名system密码不正确的解决方案
2021/10/16 Oracle
JAVA长虹键法之建造者Builder模式实现
2022/04/10 Java/Android