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

作者:诸子流 时间:2023-10-21 00:25:43 

一、说明

之前写了一篇“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

标签:python,函数名,调用,函数
0
投稿

猜你喜欢

  • Vue前端判断数据对象是否为空的实例

    2024-05-02 16:53:14
  • python实现kmp算法的实例代码

    2022-07-19 15:07:16
  • Django + Taro 前后端分离项目实现企业微信登录功能

    2023-05-31 18:48:46
  • pycharm 使用anaconda为默认环境的操作

    2023-10-08 12:37:25
  • kali-linux 202202 安装w3af命令行版的详细过程

    2023-07-14 01:34:01
  • 深入理解Python中的super()方法

    2022-11-07 05:10:45
  • Python中使用copy模块实现列表(list)拷贝

    2022-05-13 02:46:48
  • 详解Python编程中基本的数学计算使用

    2022-12-12 13:52:04
  • Python如何使用函数做字典的值

    2021-10-24 02:28:05
  • asp如何编写一个最简单的聊天程序?

    2009-11-08 19:04:00
  • SQLServer2005与SQLServer2008数据库同步图文教程

    2024-01-27 22:17:54
  • oracle 触发器 学习笔记

    2009-05-24 19:57:00
  • python如何统计序列中元素

    2022-03-04 16:32:16
  • 用"表情符号"做植入广告 是否可行呢?

    2009-02-23 13:07:00
  • sql语句返回主键SCOPE_IDENTITY()

    2024-01-12 13:14:36
  • python可视化 matplotlib画图使用colorbar工具自定义颜色

    2023-01-06 07:10:01
  • python实现简单银行管理系统

    2021-01-24 17:59:28
  • Google的用户体验设计原则

    2009-01-12 18:31:00
  • Mybatis update数据库死锁之获取数据库连接池等待

    2024-01-26 20:40:10
  • .NET Framework SQL Server 数据提供程序连接池

    2024-01-27 05:05:23
  • asp之家 网络编程 m.aspxhome.com