Python函数调用的九大方法,鲜为人知
创始人
2025-07-09 10:50:49
0

分享调用函数的9种方法:getattr, partial, eval, __dict__, globals, exec, attrgetter, methodcaller, 和 __call__。其中一些函数调用方法,在函数式编程或元编程场景中大量使用。相信你在今后的学习或工作中会遇到!

1. 直接调用函数(__call__)

最简单最直接的使用方法:

def func():
    print('Hello, world!')
func() # Hello, world!

func.__call__() # 一样的

2. partial 函数

在python的内置库functools中有一个partial函数,可以让我们可以把一个函数的一部分参数填入,然后调用。看起来没什么用,遇到的时候有大用。

from functools import partial
# 请仔细品!
def func(domain, user):
    echo = f"Hello, {user}@{domain}!"
    print(echo)


func_userA = partial(func, user="userA")
func_userB = partial(func, user="userB")
func_userA("example.com") # Hello, userA@example.com!
func_userB("example.com") # Hello, userB@example.com!

3. eval 函数

如果需要动态执行函数,可以使用 eval 来执行动态代码。

import sys
def pre_task():
    print("running pre_task")

def task():
    print("running task")

def post_task():
    print("running post_task")

cmdList = ["pre_task()","task()","post_task()"]
for cmd in cmdList:
    eval(cmd) # 执行函数
# running pre_task
# running task
# running post_task

4. getattr 函数

运行类中的静态方法

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")

#⚠️ 没有括号的字符串。
cmdList = ["pre_task", "task", "post_task"]

task = Task()

for cmd in cmdList:
    func = getattr(task, cmd)
    func()

5. dict 方法

首先我们需要知道,每个python对象都有一个内置的__dict__()方法,这个方法返回一个字典,包含了对象的所有属性。如下图,我们可以看到list的__dict__()方法返回的所有属性,其中红框内的,你是否有些熟悉?

class Task:
    @staticmethod
    def pre_task():
        print("running pre_task")

    @staticmethod
    def task():
        print("running task")

    @staticmethod
    def post_task():
        print("running post_task")


func = Task.__dict__.get("pre_task")
func.__func__() # running pre_task
func() # 为什么不这样用?

6. globals 函数

import sys

def pre_task():
    print("running pre_task")

def task():
    print("running task")

def post_task():
    print("running post_task")

cmdList = ["pre_task", "task", "post_task"]

for cmd in cmdList:
    func = globals().get(cmd)
    func()
# running pre_task
# running task
# running post_task

7. exec 函数

你可以在一个字符串中定义你的函数,并使用compile函数将它编译成字节码,然后使用exec来执行它。

# 方式1
pre_task = """
print("running pre_task")
"""
exec(compile(pre_task, '', 'exec'))
# running pre_task

# 方式2
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") # Hello, Tony
# 本文第四条
caller2 = getattr(p, "speak")
caller2("Tony") # Hello, Tony

9. methodcaller 函数

from operator import methodcaller

class People:
    def speak(self, dest):
        print(f"Hello, {dest}")

caller = methodcaller("speak", "Tony")
p = People()
caller(p)

小节

总结下,本文分享了使用函数的9种方法:getattr, partial, eval, __dict__, globals, exec, attrgetter, methodcaller, 和 __call__。

请仔细品味,思考下他们的使用场景。其中一些函数调用方法,在函数式编程或元编程场景中大量使用。相信你在今后的学习或工作中会遇到!

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...