lambda的常见用法:传递一个函数的结果作为另一个函数的参数

经常可以见到如下调用:

import thread, time
thread.start_new_thread(lambda : (thread.interrupt_main(), ), ())  

lambda在这里的作用是传递一个函数的结果作为另一个函数的参数,例如:

def perform( fun, *args ):
    fun( *args )

def action1( args ):
    something

def action2( args ):
    something

perform( action1 )
perform( action2, p )
perform( action3, p, r )

可以被方便的写成:

def Perform(f):
    f()

Perform(lambda: Action1())
Perform(lambda: Action2(p))
Perform(lambda: Action3(p, r))


你可能感兴趣的:(python)