Can you assign variables in a lambda?

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

    You can instead use a bit of creativity, for example if you want to do some evaluation to an equation and assign the result to a variable it can be done like this:

    class Results:
        res = 0
    
    clas= Results()
    setattr(clas, 'res', 3+2*4)
    print(clas.res)
    

提交回复
热议问题