问题描述
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]
>>> [my_list中的n的[square(sub3(n))]
[9,4,1,0,1,4,9,16,25,36]
不幸的是,当想将组合作为键使用时,这有点蹩脚:
>>> sort(my_list,key = lambda n:square(sub3(n)))
[3,2,4,1,5,0,6,7,8,9]
这实际上应该是 sorted(my_list,key = square * sub3)
,因为heck,函数 __ mul __
不会用于其他任何事情:
>>> square * sub3
TypeError:*:'function'和'function'不支持的操作数类型
$ b $
>>>好的,让我们来定义它吧! type(sub3).__ mul__ ='something'
TypeError:无法设置内置/扩展类型的属性'function'
D'oh!
>>> class CoolerFunction(types.FunctionType):
...传递
...
TypeError:调用元类时出错
类型'function'不是可接受的基类型
D'oh!
class Hack(object):
def __init __(self,function):
self.function = function
def __call __(self,* args,** kwargs):
返回self.function(* args,** kwargs)
def __mul __(self,other):
def hack(* args,** kwargs):
返回self。函数(其他(* args,** kwargs))
return Hack(黑客)
嘿,现在我们到了某个地方..
>>> square = Hack(square)
>>> sub3 = Hack(sub3)
>>> [my_list中的n的[square(sub3(n))]
[9,4,1,0,1,4,9,16,25,36]
>>> [my_list中的n的[(square * sub3)(n)]
[9,4,1,0,1,4,9,16,25,36]
>>>排序(my_list,key = square * sub3)
[3,2,4,1,5,0,6,7,8,9]
但我不想要一个 Hack
callable class!我没有完全理解的范围规则是完全不同的,甚至比lameda更丑陋。我想monkeypatch 功能。我可以怎么做?
你可以使用你的hack类作为装饰器,可能想为班级选择一个更合适的名称。
像这样:
b $ self_function = function $ b $ def __call __(self,* args,** kwargs):
返回self.function(* args,** kwargs)
def __mul __(self,other):
@Composable
def构成(* args,** kwargs):
返回self.function(其他(* args,** kwargs))
返回组合
def __rmul __(self,other):
@Composable
def组合(* args, ** kwargs):
return other(self.function(* args,** kwargs))
返回组合
然后你可以像这样装饰你的函数:
@Composable
def sub3 (n):
return n - 3
@Composable
def square(n):
return n * n
$ b() square * sub3)(n)
基本上你用hack类完成了同样的事情,但将其用作装饰器。
def sub3(n): return n - 3 def square(n): return n * n
It's dead easy to compose functions in 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]
Unfortunately, when wanting to use the composition as a key, it's kind of lame:
>>> sorted(my_list, key=lambda n: square(sub3(n))) [3, 2, 4, 1, 5, 0, 6, 7, 8, 9]
This should really just be
sorted(my_list, key=square*sub3)
, because heck, function__mul__
isn't used for anything else anyway:>>> square * sub3 TypeError: unsupported operand type(s) for *: 'function' and 'function'
Well let's just define it then!
>>> type(sub3).__mul__ = 'something' TypeError: can't set attributes of built-in/extension type 'function'
D'oh!
>>> class CoolerFunction(types.FunctionType): ... pass ... TypeError: Error when calling the metaclass bases type 'function' is not an acceptable base type
D'oh!
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)
Hey, now we're getting somewhere..
>>> 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]
But I don't want a
Hack
callable class! The scoping rules are totally different in ways I don't fully understand, and this is even uglier than the "lameda" arguably. I want to monkeypatch the functions. How can I do that?解决方案You can use your hack class as a decorator pretty much as it's written, though you'd likely want to choose a more appropriate name for the class.
Like this:
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
You can then decorate your functions like so:
@Composable def sub3(n): return n - 3 @Composable def square(n): return n * n
And compose them like so:
(square * sub3)(n)
Basically it's the same thing you've accomplished using your hack class, but using it as a decorator.
这篇关于如何在Python中乘以函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!