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之不要红头文件(1)
Sep 28 Python
C#返回当前系统所有可用驱动器符号的方法
Apr 18 Python
Python模块搜索概念介绍及模块安装方法介绍
Jun 03 Python
Python序列操作之进阶篇
Dec 08 Python
python中kmeans聚类实现代码
Feb 23 Python
python复制列表时[:]和[::]之间有什么区别
Oct 16 Python
python列表使用实现名字管理系统
Jan 30 Python
使用python的pexpect模块,实现远程免密登录的示例
Feb 14 Python
Python玩转PDF的各种骚操作
May 06 Python
python3模拟实现xshell远程执行liunx命令的方法
Jul 12 Python
Python全局锁中如何合理运用多线程(多进程)
Nov 06 Python
Python numpy.zero() 初始化矩阵实例
Nov 27 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
javascript some()函数用法详解
2014/11/13 PHP
PHP基于GD2函数库实现验证码功能示例
2019/01/27 PHP
用javascript实现改变TEXTAREA滚动条和按钮的颜色,以及怎样让滚动条变得扁平
2007/04/20 Javascript
jQuery 可以拖动的div实现代码 脚本之家修正版
2009/06/26 Javascript
在表单提交前进行验证的几种方式整理
2013/07/31 Javascript
js实现文章文字大小字号功能完整实例
2014/11/01 Javascript
jQuery中siblings()方法用法实例
2015/01/08 Javascript
15款jQuery分布引导插件分享
2015/02/04 Javascript
easyui导出excel无法弹出下载框的快速解决方法
2016/11/10 Javascript
Ionic+AngularJS实现登录和注册带验证功能
2017/02/09 Javascript
node.js实现的装饰者模式示例
2017/09/06 Javascript
浅谈如何使用webpack构建多页面应用
2018/05/30 Javascript
解决vue2.0路由跳转未匹配相应用路由避免出现空白页面的问题
2018/08/24 Javascript
了解javascript中变量及函数的提升
2019/05/27 Javascript
如何从头实现一个node.js的koa框架
2019/06/17 Javascript
jQuery实现轮播图源码
2019/10/23 jQuery
javascript设计模式 ? 外观模式原理与用法实例分析
2020/04/15 Javascript
使用python统计文件行数示例分享
2014/02/21 Python
Python中的列表生成式与生成器学习教程
2016/03/13 Python
Python使用django搭建web开发环境
2017/06/09 Python
利用numpy和pandas处理csv文件中的时间方法
2018/04/19 Python
浅谈python3发送post请求参数为空的情况
2018/12/28 Python
Xadmin+rules实现多选行权限方式(级联效果)
2020/04/07 Python
python新手学习可变和不可变对象
2020/06/11 Python
Python字符串及文本模式方法详解
2020/09/10 Python
python sleep和wait对比总结
2021/02/03 Python
中国旅游网站:同程旅游
2016/09/11 全球购物
应用服务器有那些
2012/01/19 面试题
触发器(trigger)的功能都有哪些?写出一个触发器的例子
2012/09/17 面试题
DataList 能否分页,请问如何实现?
2015/05/03 面试题
java程序员面试交流
2012/11/29 面试题
会计师事务所审计实习自我鉴定
2013/09/20 职场文书
优秀少先队辅导员先进事迹材料
2014/05/18 职场文书
乡镇精神文明建设汇报材料
2014/08/15 职场文书
nginx常用命令放入shell脚本详解
2021/03/31 Servers
Python中Numpy和Matplotlib的基本使用指南
2021/11/02 Python