如何在 python 中乘以函数?

How to multiply functions in python?

def sub3(n):
    return n - 3

def square(n):
    return n * n

在Python中编写函数很容易:

>>> my_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [square(sub3(n)) for n in my_list]
[9, 4, 1, 0, 1, 4, 9, 16, 25, 36]

不幸的是,要将组合用作 key 很尴尬,您必须在 another 函数中使用它们,该函数在转:

>>> sorted(my_list, key=lambda n: square(sub3(n)))
[3, 2, 4, 1, 5, 0, 6, 7, 8, 9]

这实际上应该只是 sorted(my_list, key=square*sub3),因为哎呀,函数 __mul__ 无论如何都没有用于任何其他用途:

>>> square * sub3
TypeError: unsupported operand type(s) for *: 'function' and 'function'

那么让我们定义它吧!

>>> type(sub3).__mul__ = 'something'
TypeError: can't set attributes of built-in/extension type 'function'

哦!

>>> class ComposableFunction(types.FunctionType):
...     pass
...
TypeError: Error when calling the metaclass bases
    type 'function' is not an acceptable base type

哦!

class Hack(object):
    def __init__(self, function):
        self.function = function
    def __call__(self, *args, **kwargs):
        return self.function(*args, **kwargs)
    def __mul__(self, other):
        def hack(*args, **kwargs):
            return self.function(other(*args, **kwargs))
        return Hack(hack)

嘿,现在我们到了某个地方..

>>> square = Hack(square)
>>> sub3 = Hack(sub3)
>>> [square(sub3(n)) for n in my_list]
[9, 4, 1, 0, 1, 4, 9, 16, 25, 36]
>>> [(square*sub3)(n) for n in my_list]
[9, 4, 1, 0, 1, 4, 9, 16, 25, 36]
>>> sorted(my_list, key=square*sub3)
[3, 2, 4, 1, 5, 0, 6, 7, 8, 9]

但我不想要 Hack 可调用 class!范围规则在我不完全理解的方面有所不同,而且可以说它比仅仅使用“lameda”更丑陋。是否有可能以某种方式让组合直接与函数一起工作?

Python 不(并且可能永远不会)在句法级别或作为标准库函数支持函数组合。有各种第 3 方模块(例如 functional)提供了实现函数组合的高阶函数。

您可以像写的那样将 hack class 用作装饰器,但您可能希望为 class 选择一个更合适的名称。

像这样:

class Composable(object):
    def __init__(self, function):
        self.function = function
    def __call__(self, *args, **kwargs):
        return self.function(*args, **kwargs)
    def __mul__(self, other):
        @Composable
        def composed(*args, **kwargs):
            return self.function(other(*args, **kwargs))
        return composed
    def __rmul__(self, other):
        @Composable
        def composed(*args, **kwargs):
            return other(self.function(*args, **kwargs))
        return composed

然后您可以像这样装饰您的函数:

@Composable
def sub3(n):
    return n - 3

@Composable
def square(n):
    return n * n

然后像这样组合它们:

(square * sub3)(n)

基本上这与您使用 hack class 完成的事情相同,只是将其用作装饰器。

也许是这样的:

class Composition(object):
    def __init__(self, *args):
        self.functions = args

    def __call__(self, arg):
        result = arg
        for f in reversed(self.functions):
            result = f(result)

        return result

然后:

sorted(my_list, key=Composition(square, sub3))

您可以使用 SSPipe library:

组合函数
from sspipe import p, px

sub3 = px - 3
square = px * px
composed = sub3 | square
print(5 | composed)