Python:函数可以在另一个函数的参数列表中定义吗?

2024-04-26 04:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以在python中实现这样的功能?在

another_function( function(x) {return 2*x} )

Tags: 方法功能returnanotherfunction
2条回答
def another_function( function=lambda x: 2*x ):
    print(function(10))  #an example

我不确定您发布的示例代码会发生什么,但是如果您调用another_function它将调用function(10)并打印20。在

更重要的是,不能调用another_function(7)和get 14,因为7将被分配给function和{}TypeError:“int”对象不可调用`。在

是的:

another_function( lambda x: 2*x )

需要说明的是:这是在调用another_function时发生的,而不是在定义它时发生的。在

相关问题 更多 >