网站首页 > 技术文章 正文
1.直接函数调用
这是最简单、最直观的方式:
def test():
print("This is a test")
test()

2.使用partial()函数
在 的内置库中functools,有一个专用于生成偏函数的偏函数partial。
def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
from functools import partial
power_2 = partial(power, n=2)
power_2(3) # output: 9
power_2(4) # output: 16
3. 使用 eval()
如果需要动态执行函数,可以使用 eval + string 来执行函数。
# demo.py
import sys
def pre_task():
print("running pre_task")
def task():
print("running task")
def post_task():
print("running post_task")
argvs = sys.argv[1:]
for action in argvs:
eval(action)()
执行:
$ python demo.py pre_task task post_task
running pre_task
running task
running post_task

4. 使用 getattr()
如果把所有的函数都放在类中,并定义为静态方法,就可以使用getattr()get和调用它们。
import sys
class Task:
@staticmethod
def pre_task():
print("running pre_task")
@staticmethod
def task():
print("running task")
@staticmethod
def post_task():
print("running post_task")
argvs = sys.argv[1:]
task = Task()
for action in argvs:
func = getattr(task, action)
func()
5. 使用 __dict__()
我们都知道对象有一个__dict__()魔法方法,它存储任何对象的属性和方法。
您可以调用类方法使用__dict__.get
import sys
class Task:
@staticmethod
def pre_task():
print("running pre_task")
func = Task.__dict__.get("pre_task")
func.__func__()
# Output
$ python /tmp/demo.py
running pre_task
6. 使用 global()
在 的内置库中functools,有一个专用于生成偏函数的偏函数partial。
import sys
def pre_task():
print("running pre_task")
def task():
print("running task")
def post_task():
print("running post_task")
argvs = sys.argv[1:]
for action in argvs:
globals().get(action)()
# Output
$ python /tmp/demo.py pre_task task post_task
running pre_task
running task
running post_task
7. 从文本编译和运行
您可以在字符串中定义您的函数,并使用该compile函数将其编译为字节码,然后用于exec执行它。
pre_task = """
print("running pre_task")
"""
exec(compile(pre_task, '', 'exec'))
# Or from a text file
with open('source.txt') as f:
source = f.read()
exec(compile(source, 'source.txt', 'exec'))

8. 使用attrgetter()
在 的内置库中operator,有一个获取属性的方法,称为attrgetter,获取函数后执行。
from operator import attrgetter
class People:
def speak(self, dest):
print("Hello, %s" %dest)
p = People()
caller = attrgetter("speak")
caller(p)("Tony")
# Output
$ python /tmp/demo.py
Hello, Tony
9. 使用methodcaller()
还有一个methodcaller方法在operator
from operator import methodcaller
class People:
def speak(self, dest):
print("Hello, %s" %dest)
caller = methodcaller("speak", "Tony")
p = People()
caller(p)
# Output
$ python /tmp/demo.py
Hello, Tony
猜你喜欢
- 2024-12-24 XML处理神器:Python类库让你轻松实现复杂XML操作和数据修改
- 2024-12-24 使用python实现九九乘法口诀表,使用这个语法,只需一行代码
- 2024-12-24 Python PyInstaller安装和使用教程
- 2024-12-24 pyinstaller,一个超酷的 Python 库!
- 2024-12-24 Python中Pyinstaller库的安装方法(全)
- 2024-12-24 Python音频处理的新选择:深入探索PyAudioMixer库
- 2024-12-24 PyUSB——使用Python链接USB设备
- 2024-12-24 Python+MySQL数据库操作(PyMySQL)
- 2024-12-24 Python 简单实现贪吃蛇小游戏
- 2024-12-24 不要钱,用Python识别图片中的文字
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)