How to generate a list of different lambda functions with list comprehension?

前端 未结 4 440
有刺的猬
有刺的猬 2021-01-13 23:00

This question is distilled from the original application involving callback functions for Tkinter buttons. This is one line that illustrates the behavior.

lam

4条回答
  •  有刺的猬
    2021-01-13 23:23

    Ahhh, further Googling found a solution (admittedly one I would not have stumbled upon myself). The desired behavior can be invoked by use of a default argument:

    lambdas = [lambda i=i: i for i in range(3)]

提交回复
热议问题