У меня есть две простые функции: < /p>
Код: Выделить всё
def increment(x):
return x + 1
def double(x):
return x * 2
Код: Выделить всё
double_and_increment = lambda x: increment(double(x))
< /code>
Но я также мог бы сделать это в более запутанном, но, возможно, более «эргономично масштабируемом» способе: < /p>
import functools
double_and_increment = functools.partial(functools.reduce, lambda acc, f: f(acc), [double, increment])
< /code>
Оба вышеперечисленного работают нормально: < /p>
>>> double_and_increment(1)
3
Код: Выделить всё
>>> (lambda acc, f: f(acc))(1, str) # What we want to replace.
>>> '1'
>>> import operator
>>> operator.call(str, 1) # Incorrect argument order.
>>> '1'
Код: Выделить всё
import functools, operator
# Curried form, can't figure out how to uncurry.
functools.partial(operator.methodcaller, '__call__')(1)(str)
# The arguments needs to be in the middle of the expression, which does not work.
operator.call(*reversed(operator.attrgetter('args')(functools.partial(functools.partial, operator.call)(1, str))))
Подробнее здесь: https://stackoverflow.com/questions/774 ... rd-library