如何编写返回另一个函数的函数?

时间:2021-02-13 20:57:37

In Python, I'd like to write a function make_cylinder_volume(r) which returns another function. That returned function should be callable with a parameter h, and return the volume of a cylinder with height h and radius r.

在Python中,我想编写一个函数make__volume (r),它返回另一个函数。返回的函数应该可以用参数h调用,并返回高度为h、半径为r的圆柱体的体积。

I know how to return values from functions in Python, but how do I return another function?

我知道如何从Python中的函数返回值,但是如何返回另一个函数呢?

3 个解决方案

#1


106  

Try this, using Python:

试试这个,使用Python:

import math
def make_cylinder_volume_func(r):
    def volume(h):
        return math.pi * r * r * h
    return volume

Use it like this, for example with radius=10 and height=5:

使用方法如下,例如半径为10,高度为5:

volume_radius_10 = make_cylinder_volume_func(10)
volume_radius_10(5)
=> 1570.7963267948967

Notice that returning a function was a simple matter of defining a new function inside the function, and returning it at the end - being careful to pass the appropriate parameters for each function. FYI, the technique of returning a function from another function is known as currying.

请注意,返回一个函数很简单,只需在函数中定义一个新函数,并在最后返回它——小心地为每个函数传递适当的参数。顺便说一下,从另一个函数返回一个函数的技术被称为currying。

#2


8  

Just want to point out that you can do this with pymonad

我想指出的是,你可以用pymonad

 import pymonad 

 @pymonad.curry
 def add(a, b):
     return a + b

 add5 = add(5)
 add5(4)
 9

#3


6  

Using lambdas, also known as anonymous functions, you can abstract out the volume function inside the make_cylinder_volume_func to a single line. In no way different from Óscar López's answer, the solution using lambda is still in a sense 'more functional'.

使用lambdas,也称为匿名函数,您可以将make_cylinder der_volume_func中的卷函数抽象为一行。与奥斯卡·洛佩斯的答案没有任何不同,使用lambda的解决方案在某种意义上仍然“更实用”。

This is how you can write the accepted answer using a lambda expression:

这就是如何用lambda表达式写出被接受的答案:

import math
def make_cylinder_volume_fun(r):
    return lambda h: math.pi * r * r * h

And then call as you'd any other curried function:

然后你可以调用任何其他的咖喱功能:

volume_radius_1 = make_cylinder_volume_fun(1)
volume_radius_1(1) 
=> 3.141592653589793

#1


106  

Try this, using Python:

试试这个,使用Python:

import math
def make_cylinder_volume_func(r):
    def volume(h):
        return math.pi * r * r * h
    return volume

Use it like this, for example with radius=10 and height=5:

使用方法如下,例如半径为10,高度为5:

volume_radius_10 = make_cylinder_volume_func(10)
volume_radius_10(5)
=> 1570.7963267948967

Notice that returning a function was a simple matter of defining a new function inside the function, and returning it at the end - being careful to pass the appropriate parameters for each function. FYI, the technique of returning a function from another function is known as currying.

请注意,返回一个函数很简单,只需在函数中定义一个新函数,并在最后返回它——小心地为每个函数传递适当的参数。顺便说一下,从另一个函数返回一个函数的技术被称为currying。

#2


8  

Just want to point out that you can do this with pymonad

我想指出的是,你可以用pymonad

 import pymonad 

 @pymonad.curry
 def add(a, b):
     return a + b

 add5 = add(5)
 add5(4)
 9

#3


6  

Using lambdas, also known as anonymous functions, you can abstract out the volume function inside the make_cylinder_volume_func to a single line. In no way different from Óscar López's answer, the solution using lambda is still in a sense 'more functional'.

使用lambdas,也称为匿名函数,您可以将make_cylinder der_volume_func中的卷函数抽象为一行。与奥斯卡·洛佩斯的答案没有任何不同,使用lambda的解决方案在某种意义上仍然“更实用”。

This is how you can write the accepted answer using a lambda expression:

这就是如何用lambda表达式写出被接受的答案:

import math
def make_cylinder_volume_fun(r):
    return lambda h: math.pi * r * r * h

And then call as you'd any other curried function:

然后你可以调用任何其他的咖喱功能:

volume_radius_1 = make_cylinder_volume_fun(1)
volume_radius_1(1) 
=> 3.141592653589793