【发布时间】:2011-10-10 13:12:15
【问题描述】:
有没有办法在 python 中实现这样的功能?
another_function( function(x) {return 2*x} )
【问题讨论】:
-
这段代码有什么作用?如果调用
another_function(9)是18实际传递到another_function的内容是什么?
标签: python function argument-passing
有没有办法在 python 中实现这样的功能?
another_function( function(x) {return 2*x} )
【问题讨论】:
another_function(9) 是 18 实际传递到 another_function 的内容是什么?
标签: python function argument-passing
是的:
another_function( lambda x: 2*x )
需要明确:这是在调用 another_function 时发生的,而不是在定义时发生的。
【讨论】:
SyntaxError: invalid syntax
def another_function( function=lambda x: 2*x ):
print(function(10)) #an example
我不确定您发布的示例代码会发生什么,但是如果您调用 another_function 显示的解决方案将调用 function(10) 并打印 20。
更重要的是,您不能调用another_function(7) 并获取14,因为7 将被分配给function 和7(10)' will get youTypeError: 'int' object is not callable`。
【讨论】: