When should I use function currying in Python?

前端 未结 2 552
野性不改
野性不改 2021-02-05 16:55

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

2条回答
  •  生来不讨喜
    2021-02-05 17:46

    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.

    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.

    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.

    See also Currying in Python

提交回复
热议问题