Scope of lambda functions and their parameters?

后端 未结 10 985
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 13:22

I need a callback function that is almost exactly the same for a series of gui events. The function will behave slightly differently depending on which event has called it.

10条回答
  •  粉色の甜心
    2020-11-22 13:40

    the soluiton to lambda is more lambda

    In [0]: funcs = [(lambda j: (lambda: j))(i) for i in ('do', 're', 'mi')]
    
    In [1]: funcs
    Out[1]: 
    [>,
     >,
     >]
    
    In [2]: [f() for f in funcs]
    Out[2]: ['do', 're', 'mi']
    

    the outer lambda is used to bind the current value of i to j at the

    each time the outer lambda is called it makes an instance of the inner lambda with j bound to the current value of i as i's value

提交回复
热议问题