我什么时候应该在 Python 中使用函数柯里化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24881604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When should I use function currying in Python?
提问by overexchange
When should I write my functions in curried form?does not match my thought, need to correct myself.
我什么时候应该以柯里化形式编写我的函数?与我的想法不符,需要自己纠正。
As part of my learning link, this is what I understand from function currying. Below is one example:
作为我学习链接的一部分,这就是我从函数柯里化中所理解的。下面是一个例子:
def curry2(f):
"""Returns a function g such that g(x)(y) == f(x, y)
>>> from operator import add
>>> add_three = curry2(add)(3)
>>> add_three(4)
"""
def g(x):
def h(y):
return f(x, y)
return h
return g
In any application, if I know that the number of arguments are fixed (say 2 arguments) and
function name is normalise_range
(say), then I will define def normalise_range(x, y):
function and use it in my application directly by calling normalise_range(x, y)
.
在任何应用程序中,如果我知道参数的数量是固定的(比如 2 个参数)并且函数名称是normalise_range
(比如),那么我将定义def normalise_range(x, y):
函数并通过调用normalise_range(x, y)
.
In any application, if I know that, the number of arguments are fixed (say 2 arguments),
but the function name is varying (can be normalise_range
/average
/I don't know..),
then I will use def curry2(f):
as shown above, which will accept all functions that take two arguments (fixed).
在任何应用中,如果我知道的是,参数的数目是固定的(2个说参数),但功能名称改变(可以是normalise_range
/ average
/不知..),然后我将使用def curry2(f):
如上所示,这将接受所有带两个参数的函数(固定)。
My question:
我的问题:
- Is my understanding correct?
- If yes, can we think of currying for functions of variable number of arguments?
- 我的理解正确吗?
- 如果是,我们可以考虑对可变数量参数的函数进行柯里化吗?
回答by Trilarion
The purpose of function currying is to easily get specialized functions from more general functions.You achieve this by pre-setting some parameters at a different time and keeping them fixed afterwards.
函数柯里化的目的是很容易地从更通用的函数中得到专门的函数。您可以通过在不同时间预先设置一些参数并在之后保持固定来实现这一点。
It has nothing to do with the naming. In Python you can rename a variable/function easily at all times.
这与命名无关。在 Python 中,您可以随时轻松地重命名变量/函数。
Example:
例子:
def simple_function(a):
def line(b=0):
def compute(x):
return [a+b * xi for xi in x]
return compute
return line
x = range(-4, 4, 1)
print('x {}'.format(list(x)))
print('constant {}'.format(simple_function(3)()(x)))
print('line {}'.format(simple_function(3)(-2)(x)))
gives
给
x [-4, -3, -2, -1, 0, 1, 2, 3]
constant [3, 3, 3, 3, 3, 3, 3, 3]
line [11, 9, 7, 5, 3, 1, -1, -3]
Now this was not yet that exciting. It only replaced functions calls of type f(a,b,c)
with calls of type f(a)(b)(c)
which might even be seen as the less elegant style in Python.
现在这还不是那么令人兴奋。它只是用类型f(a,b,c)
调用替换了类型的函数调用,f(a)(b)(c)
这甚至可能被视为 Python 中不太优雅的风格。
But it allows you to do:
但它允许您执行以下操作:
line_through_zero = simple_function(0)
print('line through zero {}'.format(line_through_zero(1)(x))) # only slope and x
which gives
这使
line through zero [-4, -3, -2, -1, 0, 1, 2, 3]
So the advantage of currying is that you get specialized functions that have fixed parameters and can be used instead of writing the more general form and setting the parameters fixed at each single call.
因此,柯里化的优点是您可以获得具有固定参数的专用函数,并且可以使用这些函数而不是编写更通用的形式并在每次调用时设置固定的参数。
Alternatives to currying are: partial
, lambda
and default parameters
. So in practice currying might be useful but you can also get around it if you want.
柯里化的替代方法是:partial
,lambda
和default parameters
。所以在实践中柯里化可能很有用,但如果你愿意,你也可以绕过它。
See also Currying in Python
另请参阅Python 中的柯里化
回答by Nishant
Currying has at-least two advantages I can think of:
我能想到的柯里化至少有两个优点:
1) It keeps your code (and in turn your thinking) DRY.
1)它使您的代码(以及您的想法)保持干燥。
Say you are have a function like:
假设您有一个功能,例如:
def call_me(context, args):
...
by currying you can get a specialized function for that context
which can be tossed around etc. You don't have to repeatthe context again.
通过柯里化,你可以获得一个专门的函数,context
它可以被抛出等等。你不必再次重复上下文。
2) Thinking in terms of a single input function is much easier than n
arguments; this can be debatable at times though.
2)考虑单个输入函数比n
参数容易得多;不过,这有时是有争议的。
See also: What is the difference between currying and partial application?
另请参阅:柯里化和部分应用有什么区别?