如何在 Python 中调用函数?九种方法任你挑选

开发 后端
如果把所有的函数都放在类中,并定义为静态方法,就可以使用getattr()get和调用它们。

1. 直接函数调用

这是最简单、最直观的方式:

def test(): 
    print("This is a test") 
test() 
  • 1.
  • 2.
  • 3.

2. 使用partial()函数

在 的内置库中functools,有一个专用于生成偏函数的偏函数partial。

def power(x, n): 
    s = 1 
    while n > 0: 
        nn = n - 1 
        ss = s * x 
    return s 
 
 
from functools import partial 
 
power_2 = partial(power, n=2
power_2(3)  # output: 9 
power_2(4)  # output: 16 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

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)() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

执行:

$ python demo.py pre_task task post_task 
running pre_task 
running task 
running post_task 
  • 1.
  • 2.
  • 3.
  • 4.

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() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

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')) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 

责任编辑:赵宁宁 来源: Python学会
相关推荐

2020-12-11 16:39:16

程序员编程网站

2021-08-18 11:55:25

Python函数代码

2020-09-19 18:03:42

Docker

2017-11-03 10:40:25

Python复制文件方法

2009-06-17 13:19:50

Java调用DLL

2010-04-07 15:47:32

Oracle 11g

2010-04-16 09:06:03

2023-03-07 15:47:15

2022-01-16 09:30:34

Ansible自动化工具开源

2010-04-27 15:39:54

Oracle 11g

2024-04-24 10:47:20

物联网智能建筑

2022-01-17 21:11:32

Windows 11Windows微软

2023-05-17 10:53:43

AICIO

2023-12-21 14:32:51

Python函数

2020-02-03 09:36:08

物联网智慧城市IOT

2011-07-22 12:58:16

服务器管理Android

2025-01-14 07:00:00

线程池ExecutorsJava

2012-03-27 10:08:08

JavaScript

2010-07-14 10:53:20

Web应用

2010-11-03 09:21:27

点赞
收藏

51CTO技术栈公众号