Como multiplicar funções em python?
def sub3(n):
return n - 3
def square(n):
return n * n
É fácil compor funções em 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]
Infelizmente, ao querer usar a composição como umchave, é meio coxo:
>>> sorted(my_list, key=lambda n: square(sub3(n)))
[3, 2, 4, 1, 5, 0, 6, 7, 8, 9]
Isso realmente deveria sersorted(my_list, key=square*sub3)
porque diabos, função__mul__
não é usado para mais nada de qualquer maneira:
>>> square * sub3
TypeError: unsupported operand type(s) for *: 'function' and 'function'
Bem, vamos defini-lo então!
>>> 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)
Ei, agora estamos chegando a algum lugar ..
>>> 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]
Mas eu não quero umHack
classe que pode ser chamada! As regras de escopo são totalmente diferentes de maneiras que eu não entendo completamente, e isso é ainda mais feio do que o "lameda". Quero monkeypatch ofunções. Como eu posso fazer isso?