Python 如何编写一个返回另一个函数的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14261474/
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
How do I write a function that returns another function?
提问by Julian Das
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 hand radius r.
在 Python 中,我想编写一个make_cylinder_volume(r)返回另一个函数的函数。返回的函数应该可以使用参数调用h,并返回具有高度h和半径的圆柱体的体积r。
I know how to return valuesfrom functions in Python, but how do I return another function?
我知道如何从 Python 中的函数返回值,但是如何返回另一个函数?
采纳答案by óscar López
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=10and height=5:
像这样使用它,例如与radius=10和height=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。
回答by Erotemic
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
回答by DaveIdito
Using lambdas, also known as anonymous functions, you can abstract out the volumefunction inside the make_cylinder_volume_functo 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(也称为匿名函数),您可以将volume内部的函数抽象make_cylinder_volume_func为一行。与 óscar López 的回答没有任何不同,使用 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
回答by R3ctor
I know I am too late to the party, but I think you might find this solution interesting.
我知道我参加聚会为时已晚,但我认为您可能会发现此解决方案很有趣。
from math import pi
from functools import partial
def cylinder_volume(r, h):
return pi * r * r * h
make_cylinder_with_radius_2 = partial(cylinder_volume, 2)
make_cylinder_with_height_3 = partial(cylinder_volume, h=3)
print(cylinder_volume(2, 3)) # 37.6991118431
print(make_cylinder_with_radius_2(3)) # 37.6991118431
print(make_cylinder_with_height_3(2)) # 37.6991118431
Here is documentationabout how partialworks.
这是有关如何工作的文档partial。

