【发布时间】:2022-11-12 19:42:00
【问题描述】:
您如何编写一个函数 f,它接受另一个函数 g 作为参数,但函数 g 的参数会根据函数 f 中发生的情况动态变化?
一个伪代码示例是:
def function(another_function(parameters)): # another function passed as an argument, with parameters
for i in range(10):
print(another_function(i))
所以当 i 迭代时,函数 f 每次都会用一个新的参数 i 被调用。如何实施?
我发现可以使用 *args 作为参数,但没有看到它是如何实现的。
干杯
【问题讨论】:
-
IIUC,您只需将 another_funciton 传递给函数(不带参数)。因此:
def function(another_function): ...