Annahme
class F:
def __init__(self, f):
self.f = f
def __call__(self, *args, **kwargs):
return self.f(*args, **kwargs)
def __mul__(self, other):
def _f(*args, **kwargs):
return self.f(other(*args, **kwargs))
return _f
def __ror__(self, other):
return self.f(other)
@F
def f(x):
return x + 1
@F
def g(x):
return x * x
Konsequenz
>>> 1 | f
2
>>> 2 | g
4
>>> 1 | f | g
4
>>> (f * g)(1)
2
>>> (g * f)(1)
4
Recommended Posts