Can you assign variables in a lambda?

后端 未结 7 540
礼貌的吻别
礼貌的吻别 2020-12-19 03:43

I was using a lambda statement to perform math, and happened to repeatedly use one certain value. Therefore I was wondering if it was possible to assign and use

7条回答
  •  天命终不由人
    2020-12-19 04:28

    You can just pass your lambda an argument which passes it along to another argument if you wish:

    >>> b = lambda x: 3 + 2*x
    >>> a = lambda y: y * b(y)
    >>> a(1)
    5
    >>> a(2)
    14
    

提交回复
热议问题