提问人:pms 提问时间:10/10/2011 更新时间:10/11/2011 访问量:154
Python:函数可以在另一个函数的参数列表中定义吗?
Python: can function be defined inside of another function's argument list?
答:
9赞
hamstergene
10/10/2011
#1
是的:
another_function( lambda x: 2*x )
需要明确的是:这是在调用时发生的,而不是在定义时发生的。another_function
评论
0赞
Ethan Furman
10/11/2011
这给了SyntaxError: invalid syntax
0赞
Ethan Furman
10/11/2011
没有错别字 - 以为问题是关于函数定义时间的,而你的答案是在函数调用时间。
0赞
hamstergene
10/11/2011
@EthanFurman问题恰恰是关于通话时间(答案被接受的事实不是敲响了警钟吗?作者使用了来自 Javascript 和其他基于 ecmascript 的语言的匿名函数语法。
0赞
Ethan Furman
10/12/2011
令人惊讶的是,并不是每个人都会说 Javascript 或其他基于 ecmascript 的语言(不幸的是,我看到糟糕的答案被接受);不过,我已经改变了我的投票。
0赞
Ethan Furman
10/11/2011
#2
def another_function( function=lambda x: 2*x ):
print(function(10)) #an example
我不确定您发布的示例代码会发生什么,但是如果您调用它,则显示的解决方案将调用并打印 20。another_function
function(10)
更重要的是,你不能调用和获取,因为会被分配给和 TypeError: 'int' object is not callable'。another_function(7)
14
7
function
7(10)' will get you
评论
another_function(9)
18
another_function