将函数作为参数传递给其他函数时的Python执行流
下面是“将函数作为参数传递给其他函数”的示例。我一直在努力理解这些语句的执行流程将函数作为参数传递给其他函数时的Python执行流,python,algorithm,function,Python,Algorithm,Function,下面是“将函数作为参数传递给其他函数”的示例。我一直在努力理解这些语句的执行流程 1 def plus_one(number): 2 print(f" First: {number + 1}") 3 return number + 1 4 5 def function_call(function): 6 number_to_add = 5 7 print(f" Second: {function(number
1 def plus_one(number):
2 print(f" First: {number + 1}")
3 return number + 1
4
5 def function_call(function):
6 number_to_add = 5
7 print(f" Second: {function(number_to_add)}")
8 return function(number_to_add)
9
10 function_call(plus_one)
上述报表的输出:
第一:6
第二:6
第一:6
首先调用哪个函数-“函数调用”或“加一”
根据我的理解,首先调用函数_call,参数为plus _onefn
但是,根据打印输出,首先调用一个函数。请解释此类函数调用背后的逻辑,如果可能,请与类似示例共享任何好的文档。
print(f" Second: {function(number_to_add)}")
它调用
函数
,以获取要打印的返回值<首先调用code>function\u call,但是在function\u call
中的print
之前调用plus\u one
,在第5行和第6行之间添加一个print语句,看看这是怎么回事。”……但是对plus\u one的调用是在print in function\u调用之前进行的。”哪个语句调用plus\u one或“function”?我在回答中显示的行调用了plus\u one
,它已作为参数function
传入。