How do I write a function that returns another function?

前端 未结 4 1755
悲&欢浪女
悲&欢浪女 2020-12-07 15:32

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

相关标签:
4条回答
  • 2020-12-07 15:49

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

     import pymonad 
    
     @pymonad.curry
     def add(a, b):
         return a + b
    
     add5 = add(5)
     add5(4)
     9
    
    0 讨论(0)
  • 2020-12-07 15:56

    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 documentation about how partial works.

    0 讨论(0)
  • 2020-12-07 16:12

    Try this, using 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:

    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.

    0 讨论(0)
  • 2020-12-07 16:14

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

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

    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
    
    0 讨论(0)
提交回复
热议问题