如何在python中乘以函数?

wim*_*wim 31 python monkeypatching function function-composition

def sub3(n):
    return n - 3

def square(n):
    return n * n
Run Code Online (Sandbox Code Playgroud)

在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]
Run Code Online (Sandbox Code Playgroud)

不幸的是,当想要使用合成作为关键时,它有点蹩脚:

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

这真的应该是sorted(my_list, key=square*sub3),因为heck,函数__mul__不用于其他任何事情:

>>> square * sub3
TypeError: unsupported operand type(s) for *: 'function' and 'function'
Run Code Online (Sandbox Code Playgroud)

好吧,让我们来定义吧!

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

D'哦!

>>> class CoolerFunction(types.FunctionType):
...     pass
...
TypeError: Error when calling the metaclass bases
    type 'function' is not an acceptable base type
Run Code Online (Sandbox Code Playgroud)

D'哦!

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)
Run Code Online (Sandbox Code Playgroud)

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

>>> 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]
Run Code Online (Sandbox Code Playgroud)

但我不想要一个Hack可赎回的班级!范围规则在我不完全理解的方式上完全不同,这甚至比"lameda"更加丑陋.我想monkeypatch 功能.我怎样才能做到这一点?

Jaz*_*zer 22

您可以将hack类用作装饰器,就像编写它一样,尽管您可能希望为类选择更合适的名称.

像这样:

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
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样装饰你的功能:

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

@Composable
def square(n):
    return n * n
Run Code Online (Sandbox Code Playgroud)

并像这样撰写:

(square * sub3)(n)
Run Code Online (Sandbox Code Playgroud)

基本上你使用你的hack类完成了同样的事情,但是将它用作装饰器.

  • 整齐.我做了一点改进,所以现在组合可以和任何其他的callables一起工作,例如`(sub3*int)("10") - > 7`和`(str*sub3)(10) - >'7' (4认同)