什么时候应该在Python中使用函数currying? [英] When should I use function currying in Python?

查看:95
本文介绍了什么时候应该在Python中使用函数currying?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时应以咖喱形式编写函数?与我的想法不符,需要纠正自己.

When should I write my functions in curried form? does not match my thought, need to correct myself.

作为我的学习链接的一部分,这是我从函数currying了解.下面是一个示例:

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

在任何应用程序中,如果我知道参数数量是固定的(例如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) 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/average/我不知道..), 然后我将使用如上所示的def curry2(f):,它将接受带有两个参数(固定)的所有函数.

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).

我的问题:

  1. 我的理解正确吗?
  2. 如果是,我们可以考虑使用可变参数数目的函数吗?

推荐答案

函数循环的目的是轻松地从更通用的函数中获取专用函数.您可以通过在以下位置预先设置一些参数来实现不同的时间,然后将其固定.

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.

与命名无关.在Python中,您可以随时轻松地重命名变量/函数.

It has nothing to do with the naming. In Python you can rename a variable/function easily at all times.

示例:

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)))

给予

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]

现在这还不那么令人兴奋.它只将类型为f(a,b,c)的函数调用替换为类型为f(a)(b)(c)的调用,这在Python中甚至被视为不太优雅的样式.

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.

但是它允许您执行以下操作:

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

给出

line through zero [-4, -3, -2, -1, 0, 1, 2, 3]

因此currying的优势在于,您可以获得具有固定参数的专用函数,可以使用这些函数代替编写更通用的形式并在每次调用时设置固定的参数.

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.

代替curring的有:partiallambdadefault parameters.因此,在实践中,使用粗话可能会有用,但如果您愿意,也可以解决它.

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.

另请参见使用Python进行

这篇关于什么时候应该在Python中使用函数currying?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆