如何纯粹使用Python的标准库来编写函数?

问题描述 投票:0回答:1

Python 的标准库非常庞大,我的直觉告诉我其中一定有一种方法可以实现这一点,但我就是想不出来。这纯粹是出于好奇和学习目的:

我有两个简单的功能:

def increment(x):
    return x + 1

def double(x):
    return x * 2

我想将它们组合成一个新函数

double_and_increment
。我当然可以简单地这样做:

double_and_increment = lambda x: increment(double(x))

但我也可以用更复杂但也许更“符合人体工程学可扩展”的方式来做到这一点:

import functools

double_and_increment = functools.partial(functools.reduce, lambda acc, f: f(acc), [double, increment])

以上两者都工作正常:

>>> double_and_increment(1)
3

现在的问题是,标准库中是否有工具可以实现组合而无需任何用户定义的 lambda、常规函数或类。

第一个直觉是用

lambda acc, f: f(acc)
 替换 
functools.reduce
 调用中的 
operator.call
定义,但不幸的是,这以相反的顺序接受参数:

>>> (lambda acc, f: f(acc))(1, str)  # What we want to replace.
>>> '1'
>>> import operator
>>> operator.call(str, 1)  # Incorrect argument order.
>>> '1'

我有预感,使用

functools.reduce
仍然是完成合成的方法,但我一生都找不到摆脱用户定义的 lambda 的方法。

很少有开箱即用的方法让我接近:

import functools, operator

# Curried form, can't figure out how to uncurry.
functools.partial(operator.methodcaller, '__call__')(1)(str)

# The arguments needs to be in the middle of the expression, which does not work.
operator.call(*reversed(operator.attrgetter('args')(functools.partial(functools.partial, operator.call)(1, str))))

已查看所有现有问题,但它们完全不同,并且依赖于使用用户定义的函数和/或 lambda。

python functional-programming standard-library language-features function-composition
1个回答
0
投票

好吧,既然你这么说

我想“滥用”该语言,只使用标准库中的现有定义

从Python 3.12开始,测试套件恰好包含您想要的小工具

import functools
import operator
from test.test_zipfile._path._functools import compose

increment = functools.partial(operator.add, 1)
double = functools.partial(operator.mul, 2)
increment_and_double = compose(increment, double)
print(increment_and_double(10))

(我通过本地 CPython 结帐中的战略

ag compose
发现了这一点。)

© www.soinside.com 2019 - 2024. All rights reserved.