Can you assign variables in a lambda?

后端 未结 7 569
礼貌的吻别
礼貌的吻别 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:21

    You can assign variables in lambda functions is you use exec:

    >>> a = lambda: exec('global x; x = 1')
    >>>a()
    >>>x
    1
    

提交回复
热议问题