简体   繁体   English

高阶 function 可在 python 中调用

[英]higher-order function callable in python

I have the following code which I struggle to understand.我有以下难以理解的代码。 It defines 2 functions and then it assign the the function to h, but what are X and Y?它定义了 2 个函数,然后将 function 分配给 h,但是 X 和 Y 是什么?


more info def func_max(f: Callable[[int], int], g: Callable[[int], int])-> Callable[[int], int]:更多信息 def func_max(f: Callable[[int], int], g: Callable[[int], int])-> Callable[[int], int]:

that takes as parameters the functions f and g as above.将上述函数 f 和 g 作为参数。 It returns the function h(x) that is defined on integers x and the return value of h on an integer x is equal to the maximum of m and n, where m = f(x) and n = g(x).它返回定义在整数 x 上的 function h(x) 和 integer x 上的 h 的返回值等于 m 和 n 的最大值,其中 m = f(x) 和 n = g(x)。

def f(x):
  return x**2
  
def g(x):
  return 5*x

h = max_func(f,g)
X = h(3)
Y = h(6)

The exercise is asking to assemble the following in the correct order, which I will try but it does not work, here it goes:该练习要求以正确的顺序组装以下内容,我会尝试但它不起作用,这里是:

For example I don't understand how small x enters into play now...例如,我不明白小 x 现在如何发挥作用......

def max_func(f,g):
  if f(x)> g(x): #For example I don't understand how small x enters into play now...
    return f(x)```
  else:
   return f(x)
def new_function (x:int): -> int
   return new_function

max_func should return a function that takes an argument ( x ), applies it to f and g and then return the maximal value: max_func应该返回一个 function ,它接受一个参数( x ),将其应用于fg然后返回最大值:

def max_func(f, g):
    def mf(x):
        return max(f(x), g(x))
    return mf

High order functional programming can get real weird real quickly.高阶函数式编程可以很快变得非常奇怪。 Since this seems to be a homework exercise, I won't give you the straight answer, but consider this:由于这似乎是一个家庭作业,我不会给你直接的答案,但考虑一下:

def make_function_print_arg(f):
  def new_function(x):
    print(f"Calling function with {x}")
    return f(x)
  return new_function

This defines a function that modifies the function it gets passed.这定义了一个 function 修改它通过的 function。 I can use it like this:我可以这样使用它:

def square(x):
  return x**2

print_and_square = make_function_print_arg(square)
arg = 5
ret = print_and_square(arg) # Prints out "Calling function with 5"
print(f"{arg} squared is {ret}") # Prints out "5 squared is 25"

So, make_function_print_arg is a function which takes a function f as an argument.因此, make_function_print_arg是一个 function ,它以 function f作为参数。 Within its body, it defines a new function.在其体内,它定义了一个新的 function。 This new function takes a single argument, prints that argument out, then calls f with that single argument.这个新的 function 采用单个参数,打印出该参数,然后使用该单个参数调用f Finally, make_function_print_arg returns the new function it just defined.最后, make_function_print_arg返回它刚刚定义的新 function。

Later on, we can call make_function_print_arg with a function that we've already defined, which returns a new function that's a lot like our old function, but with some modified behaviour.稍后,我们可以使用我们已经定义的 function 调用make_function_print_arg ,它返回一个新的 function ,这很像我们的旧 ZC1C425268E683854F1AB5074C17 行为,但有一些修改。

Now, in your case, you want to define a function which takes two functions as arguments, calls both of them, and returns whichever result is greater.现在,在您的情况下,您要定义一个 function ,它采用两个函数作为 arguments,调用它们,并返回更大的结果。 I'm pretty certain that one of the lines you need to unscramble should read, return g(x) , so I think either you or your teacher made a typo, but working around that, see if you can use the ideas in make_function_print_arg to manage it!我很确定您需要解读的其中一条线应该是return g(x) ,所以我认为您或您的老师打错了,但要解决这个问题,看看您是否可以使用make_function_print_arg中的想法管理它!

For your own education, you might also want to read about how decorators work, which is quite similar to what you're learning about right now: https://book.pythontips.com/en/latest/decorators.html对于您自己的教育,您可能还想了解装饰器的工作原理,这与您现在正在学习的内容非常相似: https://book.pythontips.com/en/latest/decorators.html

In your code, you are calling only f(x) in both if and else statement.在您的代码中,您在 if 和 else 语句中都只调用 f(x) 。

You can try:你可以试试:

def max_func(f,g):
  if f(x)> g(x): 
    return f(x)
  else:
   return g(x)
def new_function (x:int): -> int
   return new_function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM