One line if-condition-assignment

前端 未结 14 2624
挽巷
挽巷 2020-12-02 08:46

I have the following code

num1 = 10
someBoolValue = True

I need to set the value of num1 to 20 if someBoolV

14条回答
  •  甜味超标
    2020-12-02 09:25

    For the future time traveler from google, here is a new way (available from python 3.8 onward):

    b = 1
    if a := b:
        # this section is only reached if b is not 0 or false.
        # Also, a is set to b
        print(a, b)
    

提交回复
热议问题